Creating Manager Classes

Manager classes act as ways to encompass general settings and parameters of our games that we want to control in one place. We can have the likes of Game Managers, Audio Managers, and UI Managers.

Traditionally, we can get access to these classes from other scripts using GetComponent and accessing public methods to change things. This works, but it’s clunky, and can quickly become hard to track, especially when we need to pass variables between multiple scripts.

Instead, we can use Static Types to create an Instance of our Manager class that other scripts can then access directly through that class.

To set this up, let’s take a look at a GameManager. In it’s simplest form, we can simply create a variable of type GameManager, and assign it to itself.

We use the static identifier so that no other classes can manipulate this value.

Ultimately though, we can run into problems if we end up with more than one Instance of the GameManager. Tomorrow’s article will touch on cleaning this up with a Singleton pattern.

For now, let’s create a variable to store if our game can be won, in the form of holding a keycard to escape. We can also create a public method to manipulate this bool.

Now in our mock Player class we can call upon the GameManager directly through that Class.