UGTK / Toolkit / Design Patterns / Singleton Completed
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
-
Create a class and derive your class from
Mn_Singleton<T>whereTis your class type. Be sure of havingAwakethat callsbase.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:
-
Access the singleton instance using
MySingletonClass.Instance. -
Example usage:
```csharp
MySingletonClass.Instance.MyMethod();
```
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:
- Look at the list with the editor stopped:
Mn_SingletonDemo (2), both objects listed, and a red line saying only the first survives. - Press Play, then Scan. The count drops to
(1)and the error is gone: the loser destroyed itself inAwakeand the console says which one it was. - 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
- Script of Mn_Singleton
:
A generic singleton pattern implementation for MonoBehaviour classes, ensuring only one instance exists.
- Initialization:
On Awake, the singleton instance is managed, and duplicates are destroyed if they exist.
- Instance Management:
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
Depends on 0
- Standalone: no other module required.
Used by 24
- AppInfoStats
- EmailSender
- LeaderboardSystem
- PhpConnectionSystem
- Tickets
- SaveSystem
- 2DPlatform
- VisualNovel
- AchievementSystem
- AudioSystem
- CheckpointSystem
- DynamicTrackAudioSystem
- LanguageSystem
- LoadingSystem
- PauseSystem
- QrMultiplayerSystem
- TournamentSystem
- QuizSystem
- TangramSystem
- TrisSystem
- GestureSystem
- MessageSystem
- CodeWizard
- VibrationSystem
Measured from the repository: code references (asmdef) plus prefab and asset GUIDs. Importing this module alone brings in 0 modules in total.
UGTKengine within an engine
