Skip to main content

Singleton

Construct provides an abstract, base class extending MonoBehaviour that implements the base logic for a variation of the Singleton pattern for GameObjects' components.

A class that extends Singleton<T> (T being the class itself) automatically offers a static Instance property to access it, as well as an Exists property to check if an instance is present. See the reference for more information.

warning

Even though Systems offer the same Singleton interface, their life cycle is controlled by the architecture and not by the Systems itselves. Thus, they DO NOT extend the Singleton class.

Life cycle of a Singleton

A Singleton component exposes some parameters in the inspector to control it's life cycle.

Inspector Example

ParameterDescription
Prevent ReplacementIf checked, it prevents new instances of that type from overriding the current one if it already exists. It logs an error and destroys the newest instance. If not checked, a warning is logged (if toggled ON) and the newest instance takes precedence and the oldest one is destroyed.
Don't Destroy On LoadIf checked, the singleton is automatically marked as DontDestroyOnLoad. If not checked, the lifetime of the instance is bound to the Scene it is part of.