Creating Manager Classes in Unity — Using Singleton Pattern

Manager classes are one of the crucial components of our game. We can have many different manager classes with different responsibilities in our games; such as Audio Manager, UI Manager, Scene Manager, Game Manager and so on.

The Game Manager class is the one that controls the state of the game. In other words, every crucial part concerning the state of the game will be held in this script. In the stealth game, the game manager class will be responsible for many things, including the information if the player has gathered the card or not.

To keep our game tidy and organized, we create an empty Game Manager game object and create the script as a component to it. By default, GameManager script comes with a gear icon in Unity.

As we already know, every component that we access through GetComponent command actually creates a new instance of that component. When it comes to the Manager classes, we only need one of them running during the runtime of the game. It would not really make sense to create multiple instances of a manager class. Rather, one and only instance will be enough to be shared with all of the other classes. In order to make sure that we only have one of the manager classes in our game, we need to use Singleton design pattern.

In the most basic way, Singleton design guarantees that there will be only one instance of the class running in the game. As we know, using GetComponent to reach a component of a game object creates an instance of it; each time we use get component, we create a new instance. Imagine that you have created a game manager to be used in all of the levels of the game. Will it really make sense to have a new game manager as you pass through a new level? So how do we make sure that only one instance will be active of a manager class?

For this purpose, static keyword is used. Without using a getcomponent and creating a new instance, we have direct access to static components in the game. A static variable will be held in memory for the runtime of the game. This is actually the exact reason that we avoid unnecessary usage of static variables — you would not want your game to consume more memory, would you?

Make sure to define the GameManager instance as a private static variable so that any other class will not be able to change it — we want to make sure that it’s safe.

In order to make the private instance to be reachable by the other classes, we create a public Instance property to allow the other classes to get the private instance. By not defining any set to the property, we make sure that the private instance variable is safe within the manager class.

As we already know, Awake method is the first method that is being executed in Unity. Hence, to make sure that only one instance of the manager class is running, we change the static instance variable to the manager class itself — eliminating the previous ones.

You can check the Unity Manual regarding the execution orders in Unity from here.

Unity Developer