Understanding Unity Singleton
Using Singleton and understanding how to apply them to a game
Ever written a script where you constantly have to get the script component ever time? Better yet you keep repeating the same writing especially when you have multiple of the same script? Well Unity and C# has a solution for you and it comes in a method called “Singletons”.
What are Singletons?
Singleton are one of the best shortcuts in writing C# scripts. Once you have properly setup them up, you are able to make reference to anything within the Singleton script at any given moment of script writing. Singletons are usually used an higher tier scripts such as Manager type classes where other scripts that need data or resources from the that Manager class script can access easily.
But as usual shortcuts don’t usually come without a price, a sloppy written script with Singletons can quickly become a nightmare for developer. The reason is because Singletons don’t have any trail of where the Original script source is. Therefore it is easily to get lost if you use too many Singletons in one script. It becomes even worst when there is an error in the script and you can’t backtrack or trace back to the error since Singletons don’t leave any trail to back track. That is why Singletons are mostly use in Manager based classes since there are only one of those manager types and are easily traced back.
How to use and set up Singletons:
To start applying Singletons into your application, we first need to identify which script actually needs the singleton. As mention above, usually manger class gets the Singleton treatment, but if there is another script that is constantly referenced from other scripts you might want to try using a Singleton for that script.
In this example we will be using a Game Manager to set up our Singleton and having Player script gain a reference to it.
When creating and Singleton script we first need to make the script an “private static” variable. This is done because we don’t want Unity to create another Game Manager class randomly, the “static” is to make sure that there will always and only be ONE of the Game Manager, never more.
We will reference the Game Manager with a given name “instance”. Since this local variable is private we need to create another variable which is public that will allow the other scripts to gain access.
Within this public static variable we will need to set some information, because this is a globally accessed class we need to set up a Get function so scripts can get data from the Game Manager.
What the If statement declares is that if there isn’t any Game Manager present then we are going to Log and Error Message in the console. If there isn’t any errors, then when another script is “Getting” information from the Game Manager script we can return “this script” which is this instance. Which is why we “return instance”.
Finally in the Awake method we are going initialize this Game Manager during load time. We can do this by assigning the instance to “This” Game Manager.
Now that we have setup the Game Manager Singleton, we can start getting access to it.
How to Gain Access to the Game Manager Singleton:
Before we can move to our Player class script, we can do a quick public method within the Game Manager as an example.
In the Player class script we can how gain access to that public method in the Game Manager without using the Get component function. We simply reference the Game Manager script dot Instance (which is the singleton) and finally the public method that we desire (Player Stats).
This is a very clean and simple way of referencing a Singleton script. But as you can see, it is very hard to identify the original script if you were to bombard any script with too much singletons because you won’t be able to backtrack with the lack of information provided. Therefore it is best to keep it limited to just manager classes.