Table of Contents

Class MonoSingleton<T>

Thread-safe MonoBehaviour Singleton base class. Subclass this to create a manager or service that must have exactly one instance in the scene. The instance is auto-located via FindFirstObjectByType and optionally kept alive across scene loads via DontDestroyOnLoad.

public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour

Type Parameters

T

The concrete MonoBehaviour type that should be a singleton.

Inheritance
MonoSingleton<T>
Derived

Remarks

Usage: public class AudioManager : MonoSingleton<AudioManager> { }
Access the instance anywhere via AudioManager.Instance.
Set IsPersistent to false in the Inspector if the singleton should be destroyed when loading a new scene.

Fields

IsPersistent

When true (default), the singleton's GameObject survives scene loads via DontDestroyOnLoad. Set to false for scene-specific managers.

public bool IsPersistent

Field Value

bool

Properties

Instance

Gets the single instance of T. Lazily locates the instance in the scene if it has not been found yet. Logs an error if more than one instance exists.

public static T Instance { get; }

Property Value

T

Methods

Awake()

Unity lifecycle: assigns the singleton instance and optionally marks it persistent. Destroys duplicate GameObjects and warns in the console if more than one exists.

public virtual void Awake()

OnDestroy()

Unity lifecycle: clears the static instance reference when this object is destroyed.

protected virtual void OnDestroy()