Table of Contents

Class SceneObjectCache

A lightweight, non-singleton cache for scene objects.

  • Finds the first object of type T in the scene using Unity's FindFirstObjectByType.
  • Caches the found instance per-type to avoid repeated scene searches.
  • If the cached object gets destroyed or is missing, the cache re-finds it on the next access.

Use this when multiple systems need access to a shared scene object without forcing a Singleton pattern.

public static class SceneObjectCache
Inheritance
SceneObjectCache
Inherited Members

Methods

Clear()

Clears all cached entries.

public static void Clear()

Get<T>()

Gets a cached instance of T if available, otherwise finds the first instance in the scene, caches it, and returns it. Returns null if none found.

public static T Get<T>() where T : UnityEngine.Object

Returns

T

Type Parameters

T

Invalidate<T>()

Removes the cached entry for T, if any.

public static void Invalidate<T>() where T : UnityEngine.Object

Type Parameters

T

Set<T>(T)

Manually sets (or replaces) the cached instance for T. Pass null to clear the entry for this type.

public static void Set<T>(T instance) where T : UnityEngine.Object

Parameters

instance T

Type Parameters

T

SweepDestroyed()

Sweeps the cache and removes entries whose targets have been destroyed.

public static void SweepDestroyed()

TryGet<T>(out T)

Tries to get a cached/found instance of T.

public static bool TryGet<T>(out T instance) where T : UnityEngine.Object

Parameters

instance T

Returns

bool

Type Parameters

T