Singleton for a manager class in Unity

Daniel Kirwan
2 min readJul 13, 2021

Objective: Use the single design pattern for my manager classes in Unity

Singletons should be used sparingly and it ensures a class has one instance and you need to provide a global reference to access it.

First create a private variable that other classes will access of the singleton. In my case this will be _instance;

To avoid being able to change the value I need to create a property that will return that value. I am also doing a null check to make sure that the instance is there, otherwise it will debug the error.

In the awake method I am assigning the singleton to the class.

That is the basic setup for a singleton class. There are other things you could do like make sure that this object is passed between scenes.

Before you assign _instance = this, you could use the above code and the gameobject that is created in one scene will be taken to the next one.

In the singleton class make sure that any methods you would like other classes to access are public. All my methods are doing is loading the Main game scene and quitting the game.

So, in the Unity editor I find my buttons in the UI that will give the player the option to restart or quit. I then add a button click to the relevant buttons and drag in the UIManager gameobject that holds the script with the same name. I then select the relevant method to run for the button.

That is it for my singleton UIManager class.

--

--