How to Create a Singleton in Unity 3D

kristielTutorial, Unity Game Development

How to Create a Singleton in Unity

Share this Post

In game development, you will need to make use of global variables in many instances. Consider common gameplay elements such as player score, currency, health points, or game states such as pause or game over. Beyond these examples you will most likely need to create global variables that are specific to your game. I used Unity 3D for quite a while before I realized that there was a simple way to create and use global variables.

Singletons:  The Simple and Easy Method for Creating Global Variables

For instance, when I created a character controller entirely from scratch, I created HP and Stamina variables inside of the controller script itself. This is problematic since other objects in my game need to call on these variables at any given time in order to execute some functionality. As a result, I had to create a confusing system of methods that could be called on by using the BroadcastMessage() or SendMessageUpwards() methods. This results in horrendously confusing code and scenes. I eventually found out about Singletons, which allow a convenient way of creating global variables that can be accessed anywhere in your game. In this post, I will outline how to create a simple singleton.

How to Create a Singleton

  1. Create a new project in Unity and choose whatever settings you want.
  2. Once the project is created and the editor is open, Go to GameObject –> Create Empty. Rename the GameObject to something relevant. I named mine _GM, which stands for “Game Manager”. The underscore is just a convention I use to let me know it’s a static game object that won’t actually be interacted with in the game.
  3. Add a new C# script to the _GM item by choosing “Add Component” in the inspector. Choose New Script. Name the script “Manager” then click “Create and Add”. Open the script up in MonoDevelop or whatever IDE you use.
  4. Copy the code shown in this screenshot:
    Create a Singleton - Code Snippet
  5. Save the script.

The above code declares and initializes a public static Manager class, which gets assigned to this specific class in the Awake() function. By doing this, it allows us to call on Manager.instance in the rest of our game whenever we need it. Any public variables or methods will be able to be used. For instance, in another script, we could call Manager.instance.HP in order to get our HP value that was created in the Manager script. Let’s go ahead and do this:

  1. GameObject –> 3D Object –> Cube
  2. Select the new cube and choose “Add Component” in the inspector. Add a new C# script and call it HealthScript. Open HealthScript in your IDE.
  3. Copy this code into the script and save the file:
    Create a Singleton - Code Example
  4. Once the script is saved, go into Unity and play the game. When you press space bar, the HP from the Manager game object should decrement by 1. If you hit space until HP goes to 0, you will notice the cube in your scene gets destroyed. You can see the HP getting decremented by left clicking on the _GM game object and looking at the HP variable in the inspector.

Note:
Take note of the syntax we use to call on the HP variable in the above script. Since we initialized the public static Manager class in the other script, we are able to call on Manager.instance.HP to grab the HP variable we created in the other script. This same methodology applies to methods. For example, see these screenshots:

Create an AddHealth() method in the Manager script:
Create an AddHealth() method in the Manager script

Add an else-if statement to the HealthScript script:

Add an else-if statement to the HealthScript script:

Now when you play your game, if you press Enter/Return on your keyboard, the HP will increment by 1. Of course, this specific method is rather unnecessary, but it works for the sake of demonstration.

In conclusion, singletons are incredibly useful and powerful tools to use in your game. Ever since I found out how to use them, I never went back to my old ways. Hopefully you learned something from this. I encourage you to mess around with them and see what all you can accomplish! Don’t forget to check us out at www.studica.com!

Blogger: Mark Philipp, Application Engineer at Studica

Share this Post