SingletonBehaviour
Abstract generic MonoBehaviour singleton with a three-step lazy-initialization chain: find in scene → load from Resources → create new GameObject. Supports DontDestroyOnLoad, hierarchy/inspector visibility flags, and auto-saves a prefab asset in the editor when the asset path is configured. Extends ParagonBehaviour.
Definition
Namespace: Paragon Assembly: Paragon.dll
[SingletonSettings]
public abstract class SingletonBehaviour<T> : ParagonBehaviour
where T : SingletonBehaviour<T>Inheritance: SerializedMonoBehaviour → ParagonBehaviour → SingletonBehaviour<T>
Remarks
SingletonBehaviour<T> is the most full-featured singleton variant. Its resolution chain on first Instance access:
Return cached — If
instanceis already set, return it.Find in scene —
FindFirstObjectByType<T>(). Works when the singleton already exists (e.g., placed in a scene).Load from Resources —
Resources.Load<T>(AssetPath)and instantiate the prefab. RequiresAssetPathto be set in[SingletonSettings].Create new — Creates a
new GameObjectwithAddComponent<T>(). In the editor, automatically saves the result as a prefab atAssets/Resources/{AssetPath}.prefab.
During Awake(), the singleton:
Asserts that no duplicate instance exists
Optionally calls
DontDestroyOnLoad()based on the attributeApplies hierarchy/inspector hide flags
Quick Lookup
Access the singleton
MyManager.Instance
Define a new singleton
Subclass SingletonBehaviour<T>, add [SingletonSettings]
Persist across scene loads
Set DontDestroyOnLoad = true in [SingletonSettings]
Load from a prefab
Set AssetPath = "MyPrefab" (under Resources/)
Initialize at startup
Set InitializeOnLoad = true in [SingletonSettings]
Hide from hierarchy
Set HideInHierarchy = true in [SingletonSettings]
Properties
Instance
Static accessor for the singleton. Triggers lazy initialization on first access.
DebugEnabled
Static debug flag that delegates to the instance's ParagonBehaviour.DebugEnabled.
Extension Points
Optional Overrides
Awake()
void
Unity Awake
Instance assignment, DontDestroyOnLoad, hide flags. Call base.Awake().
OnDestroy()
void
Unity OnDestroy
Clears the static instance reference. Call base.OnDestroy().
Implementation Requirements
When subclassing SingletonBehaviour<T>, you MUST:
Apply
[SingletonSettings]to the concrete class (or inherit the base attribute)Use the CRTP pattern:
class MyManager : SingletonBehaviour<MyManager>Call
base.Awake()if overridingAwake()— it assigns the instance and applies settingsCall
base.OnDestroy()if overridingOnDestroy()— it clears the static instance
You SHOULD:
Set
AssetPathfor singletons that need persistent configuration in the InspectorSet
DontDestroyOnLoad = truefor manager objects that must survive scene transitionsSet
InitializeOnLoad = truefor singletons needed before any scene code runs
You SHOULD NOT:
Create multiple instances of the same singleton type — the
Debug.AssertinAwake()will fireAccess
InstanceduringOnDestroy()of another object — the singleton may already be nullified
Type Parameters
T
SingletonBehaviour<T>
The concrete subclass type (CRTP pattern)
Common Pitfalls
Duplicate instance assertion If two instances of the same singleton exist in a scene (e.g., from a scene reload without DontDestroyOnLoad), Awake() fires a Debug.Assert. Ensure only one instance exists, or use DontDestroyOnLoad = true.
Missing base.Awake() call If you override Awake() without calling base.Awake(), the instance field is never set, DontDestroyOnLoad is not applied, and Instance will re-trigger the initialization chain on next access (potentially creating a second instance).
AssetPath must be a Resources-relative path The AssetPath is passed to Resources.Load<T>(), so it must be relative to a Resources/ folder. Do not include Assets/Resources/ or the file extension.
Editor auto-creation saves to Assets/Resources/ When no asset exists and AssetPath is set, CreateSingleton() auto-saves a prefab at Assets/Resources/{AssetPath}.prefab. Ensure the directory exists or Unity will log an error.
Examples
Basic Manager
Hidden Background Singleton
See Also
SingletonScriptableObject<T> — ScriptableObject variant
SingletonObject<T> — plain C# object variant
SingletonSettingsAttribute — configuration attribute
SingletonInitializer — startup bootstrap
ParagonBehaviour — base class
Singletons Subsystem — subsystem overview
Last updated