intermediate Utilities

Generic Singleton

Thread-safe, persistent singleton base class for managers like AudioManager, GameManager, etc.

Unity 2022.3+ · 1.2 KB · Singleton.cs

How to Use

1

Create your manager class inheriting from Singleton:

2

public class AudioManager : Singleton { }

3

Access anywhere with AudioManager.Instance

4

The singleton persists across scene loads automatically

Source Code

Singleton.cs
C#
using UnityEngine;

/// <summary>
/// Generic singleton base class. Inherit from this to create a singleton manager.
/// Usage: public class GameManager : Singleton<GameManager> { }
/// </summary>
public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T instance;
    private static readonly object lockObj = new object();
    private static bool isQuitting;

    public static T Instance
    {
        get
        {
            if (isQuitting)
                return null;

            lock (lockObj)
            {
                if (instance == null)
                {
                    instance = FindAnyObjectByType<T>();

                    if (instance == null)
                    {
                        var go = new GameObject(typeof(T).Name);
                        instance = go.AddComponent<T>();
                    }
                }
                return instance;
            }
        }
    }

    protected virtual void Awake()
    {
        if (instance == null)
        {
            instance = this as T;
            DontDestroyOnLoad(gameObject);
        }
        else if (instance != this)
        {
            Destroy(gameObject);
        }
    }

    private void OnApplicationQuit()
    {
        isQuitting = true;
    }
}