Table of Contents

Class DontCreateNewSingleton<T>

Utility class to make inheriting types singletons.

public abstract class DontCreateNewSingleton<T> : MonoBehaviour where T : DontCreateNewSingleton<T>

Type Parameters

T

The type to be made a singleton.

Inheritance
System.Object
DontCreateNewSingleton<T>

Remarks

Assumes complete ownership of the parent GameObject.

Unlike Singleton<T>, this does not create a new instance of T if there are no existing ones.

Properties

HasInstance

Is there an existing instance of type T?

public static bool HasInstance { get; }

Property Value

bool

Instance

The singleton instance of type T.

public static T? Instance { get; }

Property Value

T

Remarks

Check HasInstance to check if an instance actually exists.

Methods

Awake()

protected virtual void Awake()

Remarks

If Instance is null, sets it to this, otherwise, destroys the current GameObject. All inheriting classes must invoke this method if overriding it.

TryGetInstance(out T?)

Tries to get the singleton T instance.

public static bool TryGetInstance(out T? instance)

Parameters

instance T

The singleton instance or null.

Returns

bool

true if an instance exists, false otherwise.