Creating Manager Classes in Unity

Objective: Create a manager class that controls the flow of the game

Jean-Noel Seneque
3 min readOct 9, 2021

In this article, I will cover why we have a Game Manager and how I used it for my latest project. In this case, it controls the flow of the game in two cases.

  1. Detects if the ‘S’ key has been pressed that skips the Intro cut scene
  2. Tracks if the keycard on the security guard was grabbed.

I have used it for other projects to help store Coins collected, scoring and a Lives counter that both the player can communicate to and for the UI to read from.

Let’s create one now. Create a script and name it GameManager. Since this script is such common practice, Unity gives this script a unique icon compared to the others.

We populate the script with the following Singleton pattern code.

The first line has a variable to create the GameManager class in memory indefinite while the game is running and that all class can see it.

To get access to this variable, we create a property and define the getter.

Now in the getter, we check that the _instance has been assigned, and if so, we pass the information otherwise something is wrong and we display an error.

Next, I may want to track if an event has occurred and I can now access this flag from any class as follows.

So what did this save me? Well normally if I want to access EventA flag, I would have to do the following.

One line of code.

This is also only possible if there is only going to be one instance of this class. That wraps up the Game Manager class.

If you enjoyed reading this article give me a Clap, also if you would like to see more, “Follow” me, so you may be notified of future releases. You may also send me a message if you need any further help.

--

--

Jean-Noel Seneque
Jean-Noel Seneque

Written by Jean-Noel Seneque

A Data & Analytics Consultant who is expanding into developing experiences in XR, Enterprise and Gaming space using Unity www.jeannoelseneque.com

No responses yet