Skip to content

Instantly share code, notes, and snippets.

@MartinZikmund
MartinZikmund / Singleton.cs
Created January 8, 2019 07:01
Singleton pattern for Unity3D
public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static readonly Lazy<T> LazyInstance = new Lazy<T>(CreateSingleton);
public static T Instance => LazyInstance.Value;
private static T CreateSingleton()
{
var ownerObject = new GameObject($"{typeof(T).Name} (singleton)");
var instance = ownerObject.AddComponent<T>();