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: SerializedMonoBehaviourParagonBehaviour → SingletonBehaviour<T>

Remarks

SingletonBehaviour<T> is the most full-featured singleton variant. Its resolution chain on first Instance access:

  1. Return cached — If instance is already set, return it.

  2. Find in sceneFindFirstObjectByType<T>(). Works when the singleton already exists (e.g., placed in a scene).

  3. Load from ResourcesResources.Load<T>(AssetPath) and instantiate the prefab. Requires AssetPath to be set in [SingletonSettings].

  4. Create new — Creates a new GameObject with AddComponent<T>(). In the editor, automatically saves the result as a prefab at Assets/Resources/{AssetPath}.prefab.

During Awake(), the singleton:

  • Asserts that no duplicate instance exists

  • Optionally calls DontDestroyOnLoad() based on the attribute

  • Applies hierarchy/inspector hide flags

Quick Lookup

Goal
How

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

Method
Return Type
Called When
Purpose

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:

  1. Apply [SingletonSettings] to the concrete class (or inherit the base attribute)

  2. Use the CRTP pattern: class MyManager : SingletonBehaviour<MyManager>

  3. Call base.Awake() if overriding Awake() — it assigns the instance and applies settings

  4. Call base.OnDestroy() if overriding OnDestroy() — it clears the static instance

You SHOULD:

  • Set AssetPath for singletons that need persistent configuration in the Inspector

  • Set DontDestroyOnLoad = true for manager objects that must survive scene transitions

  • Set InitializeOnLoad = true for singletons needed before any scene code runs

You SHOULD NOT:

  • Create multiple instances of the same singleton type — the Debug.Assert in Awake() will fire

  • Access Instance during OnDestroy() of another object — the singleton may already be nullified

Type Parameters

Parameter
Constraint
Description

T

SingletonBehaviour<T>

The concrete subclass type (CRTP pattern)

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation
circle-exclamation

Examples

Basic Manager

Hidden Background Singleton

See Also

Last updated