About Singleton- Game Dev Series 75

S.J. Jason Liu
5 min readJul 10, 2021

--

A short and quick introduction of Singleton.

We have use Singleton to create managers in our game. But what is Singleton exactly?
In this article, I will make a short introduction about Singleton with some examples.

What is Singleton

Singleton is a design pattern that would create a global-accessible class. It would only exist once in the game environment, which means it will not be copied for times.
What’s more, it would be easy for scripts to access the information, without searching the specific type in files. You do not need to use GetComponent() to access the function of Singleton.

Because it would only exist one instance in one time, the best way to use Singleton is to create a controller type of objects.

Game Manager is the best type to use Singleton.

How to create Singleton

Let’s create a game manager script with Singleton step by step.
After we create the script, delete all the method and add the first line.

In this line, we need to create an private static variable would define the game manager. However, we need to communicate with other classes in Unity. With that, we need to create a public property that accessible by other.

In this property, we need to make this can be “get” from other script, and return with this instance.

However, to prevent compiler error, we need to create a null check to debug.

One last step of setting. Since we use Singleton under MonoBehaviour, we need to assign the instance to this script at the beginning of calling.
We can use Awake() to do this part.

And that is how to create a Singleton game manager.
With any function you would like to add in game manager, you can simply create a public method.

Public method in GameManager

And it would be easy to access the method from other script.

Access the method from Player

Create Singleton object from code

Although we have created the debug line to notice us. That is only a notification, it would still crash the game. We can create the whole object from code instead of showing the debug message.

In the null check if statement, delete the debug line and create a new game object from code.

We can simply test by deleting the existing gameobject and play the game. Since Player would call the method from GameManager as what we demonstrate in the last part, it would automatically create a new gameobject from code.

Be careful! You could not use this function to create a game object with public variables or variables need to add reference from Inspector. It will cause error.

Turn a class into Singleton

So far we can create a Singleton class by steps we did above. However, it would be slow if we have tons of Singleton need to create.
We can modular this system by using MonoSingleton.

MonoSingleton can easily create a Singleton by its type of class. To start, let’s create a script called MonoSingleton. As modular system, we will create this script as a Singleton template.

First we set this to abstract class, which would be a template class. It can be heir from another script but cannot assign in Inspector.

Then we need to define the generic type by using <T>. Once we define the T as type, we need to declare what T is to MonoBehaviour, where T would set as MonoSingleton<T>.

With this line, we can simply create a Singleton by using MonoSingleton to any class.

We define the type of ControllerManager as Singleton

Next is set the private variable and property to MonoSingleton. This is the same as what we do to create a Singleton in the previous parts.

After we declare T, we can set the instance variable with T. In the null check, using “typeof” to print out T.

And also, assign instance to “this” with its type(T).

Initialized when start

Once we set class to Singleton from MonoSingleton, how do we initialize the methods like Awake() or Start()?
We can create a virtual method in MonoSingleton template that allow user to override it.

A virtual method means we can optional call it to override. And we can called it in Awake(). Once we need to initialize Awake() method from any Singleton class, we can use “override” to call this method.

When we use override in Singleton class, it would have the option of method we just created.

That’s the basic of how to use Singleton.

--

--

S.J. Jason Liu
S.J. Jason Liu

Written by S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.