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
-
objectComponentUtils
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
selfMonoBehaviourThe MonoBehaviour on which to call
GetComponent.cacheTBacking field used as the cache. Updated in-place when a component is found.
Returns
- T
The cached or freshly-found component, or
nullif not present on the GameObject.
Type Parameters
TComponent type to look up.