A Single(ton) Manager

Josh Vang
3 min readJul 13, 2021

After the creation of the managers, it’s time to pass the necessary information to objects that need them. After all, managers should be relaying important information to its subordinates. However, how do we do that to ensure information relayed are current and accurate?

The Good Way

In Unity, there are several ways to ensure the information stays current and accurate. One of these ways is to reference it in other scripts via a variable.

Game Manager script
UI Manager getting a reference to the Game Manager

In this example, the UI Manager is getting a reference to the Game Manager via a variable created in the UI Manager script.

Even though this method is good, as there’s a reference to the Game Manager, it requires other scripts to have variables ready to accept the Game Manager reference. As a result, it might become cumbersome when the game gets bigger.

The Better Way

Another way to relay information to other scripts is via Singleton pattern. Similar to the method above, using Singleton pattern also requires scripts to reference it in order to relay information from the Game Manager.

However, unlike the method above, it doesn’t require a reference variable to store the data; you can call the class directly due to only one instance of the Game Manager being created and it being stored in the memory.

Getting Started

Singleton pattern looks similar like any other scripts in Unity. However, the difference between any other scripts in Unity and a script that’s in the Singleton pattern is the inclusion of properties.

In this example, the properties are set in static so only one instance is created and can be called or modified.

Furthermore, in the public property of the Game Manager script, there’s a check to ensure the instance of the Game Manager class was created; if it does, then it will return the instance.

Lastly, by setting the instance to itself in the Awake() method, we are ensuring the instance is created once the Game Manager wakes up.

Once the properties are created, the rest of the script looks the same as any other scripts in Unity.

Referencing the Singleton

After creating the Singleton script, now it’s time to reference it. Like before, unlike the previous method, referencing a singleton method is simple:

SingletonClass.SingletonProperty.Method

There are no need to reference it externally (assigning the game object that holds the Game Manager script) or internally (via Find By Tag)

For example, in order for me to determine if the player has the keycard, I would reference it like this

GameManager.Instance.HasCard

--

--