Table of Contents

Class Singleton<T>

Namespace
Gamelogic.Extensions
Assembly
Assembly-CSharp.dll

Provides a generic implementation of a singleton-like pattern for MonoBehaviour classes. This class automatically searches for an existing instance in the scene or logs an error if none or more than one are found.

[Version(1, 0, 0)]
[AddComponentMenu("Gamelogic/Extensions/Singleton")]
public class Singleton<T> : GLMonoBehaviour where T : MonoBehaviour

Type Parameters

T

The type of the Singleton class derived from MonoBehaviour.

Inheritance
Singleton<T>
Derived
Inherited Members
MonoBehaviour.IsInvoking()
MonoBehaviour.StopCoroutine(Coroutine)
MonoBehaviour.StopAllCoroutines()
MonoBehaviour.runInEditMode
Component.GetComponent<T>()
Component.GetComponentInChildren<T>()
Component.GetComponentsInChildren<T>()
Component.GetComponentsInParent<T>()
Component.transform
Component.gameObject
Component.tag
Object.Instantiate(Object, Vector3, Quaternion)
Object.Instantiate(Object)
Object.Instantiate(Object, Transform)
Object.Instantiate<T>(T, Vector3, Quaternion, Transform)
Object.Instantiate<T>(T, Transform)
Object.Destroy(Object)
Object.DestroyImmediate(Object)
Object.DestroyObject(Object)
Object.FindObjectsOfType<T>()
Object.name
Object.hideFlags
Extension Methods

Remarks

Singletons usually manage their own creation, but it is common in Unity projects to have a singleton already placed in the scene. In this sense, this class really checks whether an instance can be used as a singleton, and then provides access to the instance.

If you want to create a singleton that is not already placed in the scene, you can use put
<xref href="Gamelogic.Extensions.SingletonPrefabSpawner%601" data-throw-if-not-resolved="false"></xref> in the scene instead.

Fields

instance

protected static T instance

Field Value

T

Properties

Instance

Returns the instance of this singleton.

public static T Instance { get; }

Property Value

T