Singleton — UI Manager

In Unity

Kenny McLachlan
3 min readSep 17, 2023

This time around, we’ll look into declaring a UI Manager. Similar to the GameManager, we’ll begin by creating an Empty Object for the UIManager and make a script for it.

The concept here is that if you are only going to have something of one, such as a Game Manager or UI Manager, you can use a Singleton to easily access information and control game logic.

To start a Singleton, we need a private variable to declare the instance of this class. It must be static so that it can run along all classes within the project.

This class is private, meaning that no other script can access it. Now we need to create a public property to access and get the private class.

We need to ensure that the _instance isn’t NULL so for best practice purposes, we conduct a null check and then return the instance.

Once we return the instance, we need to assign the instance using a void Awake().

As an example, say we wanted to have a score on our Game and we want to have the Player communicate with the UIManager and that there was a Method created called UpdateScore(int score).

Since we’re using a Singleton practice, all we need to do is call on the Instance to update the score.

Since the Player is making the score and is telling the UIManager that they’ve acquired some points, as an example, let’s say the UIManager wants to tell the Game Manager that they’ve updated the score. We would use the same practice of calling on the GameManager Instance.

This allows the Managers to communicate with each other. Note that Manager Classes cannot communicate with objects that are not singletons.

Using Singletons allow for an efficient and clean way to conduct script communication between Managers.

Thanks for tuning in!

--

--