Learning about software patterns can be confusing for beginners. You might understand the pattern but you’re not sure what to use it for. In this overview, we will show you several real world examples of how you can use the Singleton Pattern in Unity


The Singleton Pattern
The Singleton Pattern is a Design Pattern to create a class that that manages an instance of itself. At any time, there can only be one instance of this class and it’s usually implemented using a static variable.
In Unity, we can use Singletons for classes that typically have one instance. This makes them suited for creating manager classes and even some GameObjects. An advantage of using a singleton in Unity is that we no longer rely on methods such as GetComponent or FindObjectOfType. Instead, we can access the static variable of the singleton.
Singleton Pattern Examples in Unity
Singleton C# Class
The simplest way to implement a Singleton in Unity is by creating a C# class like below. Notice how we can create a singleton in Unity without a MonoBehaviour.
To access the instance of the class, simply call the GetInstance method. By default, the singleton does not have an instance yet it’s created the first time the GetInstance method is called.
Advantages
- No dependencies on Unity
- The Singleton exists across scenes
Disadvantages
- Does not receive updates from Unity through Update or LateUpdate
- Not usable with Unity Inspector
public class Singleton { private static Singleton instance; public string message = "Hello World"; public static Singleton GetInstance() { if(instance == null) { instance = new Singleton(); } return instance; } }
Now that we have implemented a singleton, we can use it anywhere in the code. Note that we never have to instantiate the Singleton class anywhere. The code does this automatically for us in the GetInstance method. Also, because we are not relying on a MonoBehaviour for this implementation we can use this Singleton across scenes.
// Print Hello World to the Unity Console string message = Singleton.GetInstance().message; Debug.Log(message);
MonoBehaviour Singleton in Unity
The most popular way to use a singleton in Unity is through a MonoBehaviour and adding it to a GameObject in the scene.
Below, you can find the simplest example of making a singleton in Unity through a MonoBehaviour base class. In order to use our new Singleton class, we have to attach the Singleton script to a GameObject in a scene.
Advantages
- Has all the methods a normal MonoBehaviour has (Awake, Start, Update, LateUpdate, etc..)
- Editable through the Unity Inspector
Disadvantages
- Lifetime management is more complex across scenes.
- Creates dependencies on Unity code.
- Messy Execution Order
- No Constructor Available
using UnityEngine; [DefaultExecutionOrder(-1000)] public class Singleton : MonoBehaviour { private static Singleton instance; public string message = "Hello World"; private void Awake() { if(instance == null) { instance = this; } } public static Singleton GetInstance() { return instance; } }
When looking at the implementation above, you will notice the Awake method actually sets the static instance variable.. This is different compared to the previous implementation where the GetInstance method automatically creates a new instance for us. The reasoning behind this is that while we can’t access ta MonoBehaviour constructor. We have to rely on the Awake method being called on the MonoBehaviour before we can set the instance.
Unity Singleton Execution Order
To ensure the Awake method of the Singleton is called before any other Awake method, we add the DefaultExecutionMethod attribute to our Singleton. Using this Unity Attribute, we have more control over execution order of various scripts. If you are experiencing a null reference exception with your MonoBehaviour Singleton classes, make sure you check the execution order of your scripts.
Example of Using a MonoBehaviour Singleton in Unity
In the example below, we use a Player class to output a Hello World message to the Unity Console in its Awake method.
Player.cs
public class Player : MonoBehaviour { private void Awake() { // Output Hello World to the Unity Console string message = Singleton.GetInstance().message; Debug.Log(message); } }
ScriptableObject Singleton in Unity
Another way to make a singleton in Unity is through a ScriptableObject. instead of using the MonoBehaviour as a base class. However, to use a ScriptableObject Singleton, you need to create an actual asset for it using the Create Menu.
In the snippet below, you can find an example of a singleton implemented as a ScriptableObject. Notice how this time we are using the OnEnable method. This method is called the moment the asset of our ScriptableObject Singleton is loaded into memory at the start of the game..
Advantages
- Editable through the Unity Inspector
- Does exist outside scenes.
Disadvantages
- You have to create an asset for the ScriptableObject.
- You can only have one ScriptableObject asset per type in your Unity Project.
using UnityEngine; [CreateAssetMenu(menuName ="Singletons/ScriptableObject Singleton")] public class ScriptableObjectSingleton : ScriptableObject { public string message; private static ScriptableObjectSingleton instance; private void OnEnable() { if(instance == null) { instance = this; } } public static ScriptableObjectSingleton GetInstance() { return instance; } }
Again, using the new Singleton is a very straightforward process. In the example below we are using our ScriptableObject singleton to output a Hello World message to the Unity Console:
Player.cs
public class Player : MonoBehaviour { private void Awake() { // Set message in the singleton ScriptableObjectSingleton.GetInstance().message = "Hello World!"; // Output message to the Unity Console string message = ScriptableObjectSingleton.GetInstance().message; Debug.Log(message); } }
Real World Singleton Pattern Examples in Unity
Game Manager Singleton in Unity
In a lot of games, you will find a Game Manager class that is implemented through a Singleton pattern. The reason for this is that a Game Manager class plays central role in any game architecture and often needs to be accessed by other systems.
In the example below, we have a simple implementation of a Game Manager. This Game Manager features a method to manually restart the game. Next to that, it also monitors the player and attempts to restart the level when the player dies.
GameManager.cs
using UnityEngine; using UnityEngine.SceneManagement; [DefaultExecutionOrder(-1000)] public class GameManager : MonoBehaviour { [SerializeField] private Player player; private static GameManager instance; private void Awake() { if(instance == null) { instance = this; DontDestroyOnLoad(this.gameObject); } } private void Update() { if(!player.IsAlive()) { Restartlevel(); } } public void Restartlevel() { Scene scene = SceneManager.GetActiveScene(); SceneManager.LoadScene(scene.name); } public static GameManager GetInstance() { return instance; } }
Persistent Singletons using DontDestroyOnLoad
As can be seen above, we extend the lifetime of a MonoBehaviour Singleton in Unity through the DontDestroyOnLoad method. By using this method, the GameObject which hosts the Singleton script will not be destroyed whenever a new scene is loaded. In our case, this makes our Singleton persistent across you entire game.
UI Manager Singleton in Unity
Another common use of a Singleton in Unity is that of a UI Manager. In the example below, we have implemented a UI Manager using a ScriptableObject Singleton. This way, the singleton persists through our game levels and is always accessible whenever we need it.
UIManager.cs
using UnityEngine; [CreateAssetMenu(menuName ="Singletons/UI Manager")] public class UIManager : ScriptableObject { [SerializeField] private GameObject startMenu; [SerializeField] private GameObject gameOverMenu; [SerializeField] private GameObject optionsMenu; private static UIManager instance; private GameObject recent; private void OnEnable() { if(instance == null) { instance = this; } } public void ShowStartMenu() { ShowMenu(startMenu); } public void ShowGameOverMenu() { ShowMenu(gameOverMenu); } public void ShowOptionsMenu() { ShowMenu(optionsMenu); } private void ShowMenu(GameObject menuPrefab) { Destroy(recent); recent = Instantiate(menuPrefab); } public static UIManager GetInstance() { return instance; } }
AI Blackboard Singleton in Unity
Finally, another common use for a singleton is to employ it for a blackboard. Similar to a Singleton, a blackboard is another design pattern that tries to address another common problem in software development. Compared to the Singleton, the main aim of the blackboard pattern is to to group a collection of data so that it is easily accessible across the project.
Often, you will see a blackboard pattern being used in the AI domain to create a data set used by agents for decision making. In the snippet below, you can see how our blackboard singleton functions as a way to retrieve the player and all available pickups in the current level. Whenever an agent needs to make a decision, it can access our blackboard singleton without any problems.
Blackboard.cs
using System.Collections.Generic; using UnityEngine; [DefaultExecutionOrder(-1000)] public class Blackboard : MonoBehaviour { [SerializeField] private Player player; [SerializeField] private List<Pickup> pickups; private static Blackboard instance; private void Awake() { if(instance == null) { instance = this; } } public Player GetPlayer() { return player; } public Pickup[] GetPickups() { return pickups.ToArray(); } public static Blackboard GetInstance() { return instance; } }
Best Practices
While Singletons look incredibly useful and harmless they should also be used with care. When using a Singleton in Unity, use the following best practices as a guideline:

- Always make sure you check the execution order of any singleton classes derived from MonoBehaviour.
- Differentiate between Singletons that exist during levels and Singletons that exist throughout the entire game.
- Resist the urge to turn every single class into a singleton. Instead, focus your efforts on implementing your Manager classes as Singletons.
- Resist the urge to change a class into a singleton just because there is no way for other classes to access it.
Summary
What you learned in this article:
- The Singleton Pattern is a Design Pattern to create a class that that manages an instance of itself
- An advantage of using a singleton in Unity is that we no longer rely on methods such as GetComponent or FindObjectOfType.
- When using a MonoBehaviour singleton, we have to rely on the Awake method being called on the MonoBehaviour before we can set the instance.
- If you are experiencing a null reference exception with your MonoBehaviour Singleton classes, make sure you check the execution order of your scripts
- To use a ScriptableObject Singleton, you need to create an actual asset first.
- By using the DontDestroyonLoad method, the GameObject which contains the Singleton script will not be destroyed whenever a new scene is loaded

Conclusion
To conclude, we hope you now have a better intuitive understanding of the Singleton Pattern and how to use it in Unity. In this overview, we have shown how to use the Singleton Pattern and several real world examples. If you found this article helpful, please share it.
Further Reading
Other articles you may find interesting:
