Manager Classes- Game Dev Series 74

Objective: create manager classes in Unity.

So far our stealth game have created more than 10 scripts to handle all the functionality. We used to communicate between scripts by accessing each other.
However, we can easily to do this by creating a manager.

A manager is an unique script to handle all the instances that you want to process in game. It can be many managers in game to process different function. For example, game manager can handle the system function of game progress; audio manager can run the audio beyond the gameobjects.

In this article, we will create 2 managers to run make our game system runs easier.

Game manager

The first function we want the game manager to handle, is the bool of Player holding the keycard.

We have finished this part of cutscene trigger in the previous article. Before we move on the game progress, we need to make the system to check if our Player is holding keycard to the ending cutscene.
Although we can simply make the ending cutscene trigger to detect is the Player holding the keycard by communicating another script, it would be faster if we let a central system to do this process.

Now we can create the game manager. Create an empty object with a new script called GameManager.
In script, we will create a coding pattern called “Singleton”, which we will explain it more in the next article.

To create a singleton, we will create a private instance to access GameManager. And we also need to create a public property to retrieve the instance.

To access the instance under MonoBehaviour, we will need to assign the instance to this script by using Awake().

Now we have the basic of GameManager.
Next we will create the instance of the card detecting bool.

With this instance, we can read the current status(get), and rewrite a new status(set).

That is what we will use for both of trigger scripts. In the GrabKeyCardActivation script, access the instance in the OnTriggerEnter by using the following code.

This is the biggest profit of using singleton. You do not even need to make the GameManager as SerializeField or public, or even access it by using GetComponent() in script. Just one line to achieve the same result.

Next we will check if the bool instance is true in another trigger script.
Add an if statement in the OnTriggerEnter().

The concept of using singleton would be like this.

Next we will set the audio manager.

Audio manager

As its name, it will handle the audio playing function. The first thing that we could combine the function with audio manager, is the voice over triggers.

Create an empty gameobject and the script of AudioManager as GameManager.

Under the AudioManager gameobject, create a new empty called VO as voice over player. Add an Audio Source component to it.

In the AudioManager script, create a variable to control the audio source. And create an instance method to play the audio.

Now we can open the voice over script to access this instance method.

Again, just one line to access the instance. The best part of manager system is every script can receive the variables from it with only one line of code, without setting references.

Now your voice over trigger should work as usual, but with a cleaner code.

This is the basic of manager system. In the next article, we will figure more about singleton.



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