Unity Guide
Implementing a game manager using the Singleton pattern | Unity
A quick guide about how to implement a game manager using the Singleton pattern with Unity
Objective: Implement a game manager instance by applying the Singleton pattern in Unity to manage a simple game mechanic.
In the last post I covered how to trigger a finished cutscene and return to the game with Unity. Now, it’s time to implement a game manager to handle a simple condition by applying the Singleton pattern with Unity.
Creating the game manager
In order to implement the game manager, let’s start by creating a new script:
Now, let’s create a new empty gameobject for the game manager and let’s attach the respective script through the inspector:
Applying the Singleton pattern
The Singleton pattern implies that there’s only going to exist one unit of the class (in which the pattern is applied) within the scene. In this case, the Singleton pattern can be useful to assure that there will only be a single manager of each kind (A game manager, an audio manager, a UI manager, an AI manager, etc.), so we can use it to handle the game manager of our game.
So, in order to apply the Singleton pattern, let’s open the Game Manager script. Let’s start by creating a new private (to assure that only the class can modify it) and static (to assure that it’s shared through all the instances of this class) variable of type GameManager that will hold the respective unique instance of the class:
Then, in order to be able to access the unique instance from other classes, let’s create a new public property with just a get option. In the get option we’ll check if the private static instance from above is null to state that the game manager is null (if that’s the case), then, we’ll return the respective instance to be read:
Now, in order to initialize the private instance, we’ll need to use the Awake/Start method:
Note: We could also use lazy instantiation to create the respective manager when we need to use it in the game and improve performance.
Finally, in order to show a possible use of the game manager singleton, let’s create a new auto public bool property that can be accessed or modified with get and set respectively. This property will define if the player has already acquired the card from the sleeping guard to complete the level:
Modifying the property of the instance
If you read the last post, you’ll remember that we triggered the cutscene where the player grabs a keycard from a sleeping guard using the OnTriggerEnter method. So, in order to define that the keycard is grabbed, we can choose to modify the property by calling the game manager instance and its property from the class that triggers the cutscene:
This way, we’ll be able to check if the last cutscene should be triggered or not depending if the player has the keycard.
Checking the property of the instance
So, let’s apply the same mechanic (as in the last post) to the last cutscene. We have a gameobject with a collider and a rigidbody to trigger the last cutscene when the player approaches the door:
Then, using the OnTriggerEnter method, we’ll be able to check if the keycard is already grabbed by accessing the game manager instance and its properties:
If we grab the respective keycard and try to trigger the last cutscene, we’ll notice that the property from our game manager instance works as expected:
And that’s it, we implemented a game manager by applying the Singleton pattern with Unity! :D. I’ll see you in the next post, where I’ll be showing how to create a loading scene with Unity.