Day 72 — Creating Manager Classes in Unity

Connor Fullarton
4 min readMay 26, 2021

Hey and welcome!

I’ve got something truly fascinating for you in this one. We’re going to be diving into looking at how to use our Manager Classes with a singleton design pattern. I’ll go over singletons in depth in the next article so we can focus more on what we’re able to do with our Manager Classes by using a GameManager class to handle whether the player has the key card or not as you may have noticed that we can’t end the level currently.

Let’s start things off by creating the GameManager class which can be done by creating a new C# script and naming it GameManager. Unity will automatically add a little gear icon to the file instead of the usual script icon. With that created go ahead and add in the following code to get started with your first singleton:

private static GameManager _instance;
// This is a property
public static GameManager Instance
{
get
{
if (_instance == null)
{
Debug.LogError("GameManager is null");
}
return _instance;
}
}
// This is an auto-implemented property
public bool HasCard { get; set; }
private void Awake()
{
_instance = this;
}

To start things off we’re making use of our first instance using static when defining a variable. Static allows variables to be accessed through every class as it adds it to the memory indefinitely. Other than that it’s a pretty normal variable.

Our next variable is a bit more interesting and that’s because it isn’t actually a variable and is instead known as a property. We’ve had a little experience using properties but not making them, if you have tooltips enabled you may have noticed some methods you’ve used have had “Get” and “Set” in them, those were defined by using a property.

So in our first every property here we’re defining Get, null checking our _instance variable and then returning it. You’ll notice that our property here is public and that’s because this is what we’re going to be referencing when we want to access this from a different script rather than our _instance variable directly.

Next up we’re creating something called an auto-implemented property which is just a more concise and readable form of a property that we can use so long as we don’t need to define additional logic within Get or Set. We wouldn’t be able to make our Instance property one since we have our if statement and our return inside the get. We’ll be using HasCard later but just know that we have the ability to either get or set the value of it.

Lastly is our Awake() method, this is one that gets called when the script itself gets loaded rather than just when the game starts for Start(). Inside the method we’re then assigning our _instance variable to our script while it’s attached to an object. Speaking of, go ahead and create an empty object called GameManager and attach the script to it so that this method works!

Ok that’s the text heavy part done, let’s see this code put to use by adding in the following line to the player collision in our GrabKeyCard script:

GameManager.Instance.HasCard = true;

Now when the collision between the sleeping guard cutscene trigger and the player happens our HasCard property is set to true.

Next up is making use of HasCard being set to true. In the level colliders object you’ll see a Win_Cut_Scene_Zone object inside it which has a trigger box set right in front of the door at the end of the level.

Create a script called WinStateActive or similar that you can attach to this object and then add in this code:

public GameObject winningCutscene;void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
if (GameManager.Instance.HasCard == true)
{
winningCutscene.SetActive(true);
}
else
{
Debug.Log("You must have the KeyCard in hand");
}
}
}

The object that you’ll want to put into winningCutscene is the one that you’re using to hold the cutscene you want to play when Darren reaches the door and wins the level.

In this collision we’re accessing the game manager and “Getting” HasCard in order to check if its value is true.

Pretty fascinating stuff right? If you were to have done this without the singleton design pattern then you would have had to add in extra lines of code in order to first get a local reference to GameManager by using GetComponent. Certainly makes life a bit easier if you need to make a lot of references to the GameManager!

--

--

Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.