Skip to content

Singletons

Ilia Petrov-Komotskii edited this page Oct 11, 2026 · 4 revisions

Three Unity-side singletons, plus the engine-free ManeSingleton in ManeTools-dotNET.

Type Backing When to use
ManeSingleton<T> Plain C# Domain code, no GameObject
UnitySingleton<T> MonoBehaviour Needs a scene object / Update
ScriptableSingleton<T> ScriptableObject Data asset, or a hidden runtime instance

UnitySingleton

First access to Instance finds a scene component (including inactive) or creates a DontDestroyOnLoad object named [TypeName]. SetInstance / ClearInstance / IsReady() match the ManeSingleton.

using Mane.Unity;
using UnityEngine;

public class AudioHub : UnitySingleton<AudioHub>
{
    public void Play(AudioClip clip) { /* ... */ }
}

AudioHub.Instance.Play(clip);

A second instance destroys its GameObject in Awake when another is already registered, for example when the scene that holds the singleton is loaded again. If you override Awake, call base.Awake() first and return when Instance != this. Destroyed Unity objects count as not ready.

ScriptableSingleton

Looks for a loaded asset first, otherwise creates a hidden runtime instance (HideAndDontSave). Prefer assigning an asset and calling SetInstance (or cloning it yourself) so play mode does not mutate the project asset.

Clone this wiki locally