Table of Contents

Class ComponentUtils

Namespace
ElectricDrill.AstraRpgFramework.Utils
Assembly
com.electricdrill.astra-rpg-framework.Runtime.dll

Extension methods for MonoBehaviour component caching.

public static class ComponentUtils
Inheritance
object
ComponentUtils

Methods

GetOrCache<T>(MonoBehaviour, ref T)

Returns the cached component reference if valid, otherwise fetches it via UnityEngine.Component.GetComponent<T>() and caches the result.

The "if found" guard on assignment is intentional: it preserves compatibility with test mocks that do not inherit from Object and would therefore fail Unity's fake-null check (!cache) even when the field is set.

public static T GetOrCache<T>(this MonoBehaviour self, ref T cache) where T : Component

Parameters

self MonoBehaviour

The MonoBehaviour on which to call GetComponent.

cache T

Backing field used as the cache. Updated in-place when a component is found.

Returns

T

The cached or freshly-found component, or null if not present on the GameObject.

Type Parameters

T

Component type to look up.