Day 82: Game Programming Pattern: Singleton

Objective: Understand how to use the Singleton pattern better.

As mentioned yesterday in the Manager Class article, a Singleton pattern allows for global access with only a single script in the game. In each manager class we create we will use the same pattern of code.

For this example we will create a spawn manager. We start by creating a static variable type of SpawnManager called _instance. Next we will create a public static variable type of SpawnManager that uses properties to check and see if the instance is null, if it is it’ll send in error and if it is not, it’ll return the _instance.

Now to set the instance to this object, we will use the Awake function that Unity provides and set the _instance = this.

For an example, on a random object there is a script to start spawning enemies(right now it just use debug.log to send a message to the console.). To start spawning enemies we need a public function inside the SpawnManager for the other script to call.

Now on the other script we just call the SpawnManager instance and call the function in a single line of code. SpawnManager.Instance.StartSpawning();

This is how to use the Singleton pattern. But what if you have multiple Manager classes that implement this pattern? A common design pattern to do when coding is not to write the exact code multiple times. We can fix this by creating a MonoSingleton class. This MonoSingleton class will be a generic abstract class.

To start we create another C# script named MonoSingleton. Once you open it up, delete the start and update functions. Add abstract in front of class. Next a the generic <T> after MonoSingleton and after MonoBehaviour add Where T : MonoSingleton<T>. The T will allow us to pass in any script to use this pattern.

Now to create the static variables. We will do the same thing as seen above but now we will change the Manager name to ‘T’. ‘T’ will be replaced by the class name when it gets called. Next thing we need to do is print out the Manager class that is null but since we are using a generic class, we don’t know which one it is. To know which one is null, we will use typeof().ToString(), to print out the name of the class that is null.

Next it to set the instance in Awake. It is almost the same as before but just have to cast this as T to set the instance.

Now our generic class is set up, let’s implament it onto our Spawn Manager class. The first thing we need to do is remove MonoBehaviour and replace it with MonoSingleton<SpawnManager>. We will remove the Singleton pattern from the Spawn Manager class as well.

This is how to use and simplify the Singleton Pattern. Thank you for reading and have a wonderful day!




I am passonate on learning to program and use Unity to become a skillful Unity Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Building Jarvis — -not really

Oracle GoldenGate MySQL to MySQL Configuration part 2

Launch an EC2 instance in AWS Cloud and Host Static webpage

Basics of serverless functions by the example of Azure Functions + Hands on

Augmented Reality in Unity3D — how to get started

Django 101 [Updated]

Web Services (What are REST Web Services)-Part(4)

Striver SDE sheet: Majority Element approach and solution

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tyler Smallwood

Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer

More from Medium

GamePad Support w/ New Unity Input System

Day 111: Introduction to Tilemap in Unity

Creating Elevator in Unity — Part 3: Moving Elevator.

Spawning objects in unity without the clutter