Generic Singleton
Thread-safe, persistent singleton base class for managers like AudioManager, GameManager, etc.
No APIs that Unity 6.0 to 6.6 made obsolete or removed. How this is checked
What it adds to your GameObject
A MonoBehaviour component: attach it to a GameObject.
No serialized fields. This is a static helper or a base class: you call it from code rather than configure it in the Inspector.
When to use it
Use whenever you need a single global manager that persists across scenes — GameManager, AudioManager, SaveManager, InputManager, etc. Perfect for any game architecture that requires centralized systems accessible from anywhere via a static Instance property. Especially useful in larger projects with multiple scenes.
Setup
- Create your manager class inheriting from Singleton<T>:
- public class AudioManager : Singleton<AudioManager> { }
- Access anywhere with AudioManager.Instance
- The singleton persists across scene loads automatically
Features
- Generic base class — inherit to make any MonoBehaviour a singleton
- Thread-safe access with lock object for multi-threaded scenarios
- Auto-creates instance if none exists in the scene
- DontDestroyOnLoad persistence across scene changes
- Duplicate destruction — extra instances are automatically removed
- Application quit guard prevents ghost instances during shutdown
Requirements
- Calls DontDestroyOnLoad, so the object survives scene loads. Place it once, in your first scene.
Common mistakes
Placing two GameObjects with the same Singleton subclass in a scene will cause one to be destroyed immediately — this is intended, but can be confusing. If you override Awake() in your subclass, you must call base.Awake() or the singleton setup breaks. Accessing Instance during OnDestroy or OnApplicationQuit may return null due to the isQuitting guard.
Source code
Singleton.cs, 54 lines, MIT licensed. Paste it into a new C# file of the same name.
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;
}
}