UGTK cat mascotUGTKengine within an engine Request access

UGTK / Toolkit / Design Patterns / Singleton Completed

Design Patterns — UGTK

Singleton Pattern

Summary

The Singleton Pattern module within the Unity Games Toolkit (UGTK) provides a generic implementation for Singleton classes in Unity. It ensures that only one instance of a singleton class exists throughout the game, managing the creation of the instance across scene loads. If more than one instance is found, the duplicate is destroyed.


Content


Modules Dependencies

The Singleton Pattern module does not have external dependencies within the UGTK. It can be used independently.


Setup

  1. Create a class and derive your class from Mn_Singleton<T> where T is your class type. Be sure of having Awake that calls base.Awake:

    ```csharp

    using UGTK.DesignPattern.Singleton;

    public class MySingletonClass : Mn_Singleton

    {

     // Your class implementation here
    
     //Awake
    
     public void Awake()
    
     {
    
          base.Awake();
    
     }
    

    }

    ```


How To Use

Access the Singleton Instance:


Testing Scene

Open Debug/SingletonScene.unity. It holds nothing but two Mn_SingletonDemo objects, Game Manager A and Game Manager B, because the thing worth testing here is a failure rather than a feature.

Then open Tools > UGTK > Singletons. The window scans the open scenes as soon as it appears.

What to do, in order:

  1. Look at the list with the editor stopped: Mn_SingletonDemo (2), both objects listed, and a red line saying only the first survives.
  2. Press Play, then Scan. The count drops to (1) and the error is gone: the loser destroyed itself in Awake and the console says which one it was.
  3. Stop and Scan again. Both are back, because the scene on disk still has two.

Step 2 is the whole point, and note which one survives: the winner is whichever Awake ran first, which is not something the hierarchy order guarantees. Every inspector reference wired to the other one is now null, and nothing in the console connects the two facts - it surfaces later as a manager that stopped working.

The window is considerably faster than finding duplicates by hand, and it is the reason this module ships one: a duplicate singleton is invisible until something else breaks.


Technical Info

A generic singleton pattern implementation for MonoBehaviour classes, ensuring only one instance exists.

On Awake, the singleton instance is managed, and duplicates are destroyed if they exist.

If the singleton instance does not exist, it is either found in the scene or created. It does not survive a scene load by itself: call DontDestroyOnLoad in your subclass if it must outlive the scene.

For detailed documentation of the script, refer to the corresponding script file.


© 2026 Marcello De Bonis. All rights reserved

Real dependenciesOpen in the map →

Measured from the repository: code references (asmdef) plus prefab and asset GUIDs. Importing this module alone brings in 0 modules in total.