Creating Manager Singleton in Unity

Mike Brisson
3 min readJun 9, 2021

Creating a manager class and needing references to that class can be a slow and tedious task. We would typically define a class to be the manager, cache a reference to it, and make whatever call we need…like this:

It’s ok to do this when attempting to initially solve a problem, but it isn’t ideal with production ready code.
A better approach is to create a manager class using the Singleton method. This allows for 1 class to be accessible to all other classes for the duration of the program.

It starts by declaring a private static variable of type <GameManager> so that no other class have access to this variable. Then we give access with a property like so:

Setting up the public property “Instance” this way allows for any class to access the variable without being able to change or “set” it. If you wanted to, you could also call “Set” within the property if you wanted to add some functionality, but usually you just want to return the instance.

An even cleaner way of doing this is to use an expression-bodied format like this:

The next thing we want to do is to make sure there is only one GameManager and assign the _instance to the current game object:

The last thing we need is to assign _instance to this object if it is indeed the only one…leaving us with this completed code:

Now we can set up a property within the GameManager and call it directly from any other class:

After defined in the manager class, we call it using ‘.Instance.{Property}’:

--

--