Game Manager in Unity

Objective: Create Manager Classes in Unity

Up till now we have used a script on a GameObject called GameManager. This time we’ll take up up a notch and use a Game Manager Class using a Singleton Pattern.

We will create an empty object and call it GameManager. Next create a script called GameManager. Unity automatically knows this is a special type of script and even gives it, it’s own icon to identify it.

We can then attach the script to the GameManager object. We are only ever going to have a single instance of the GameManager in the scene and that makes this a perfect time to use the Singleton Pattern.

The first thing in the script is a bit of cleanup. We remove the default Start and Update methods we get when we create a script in Unity. Next we need to have a static class for this to work. This static class allows it to be accessible to all classes without the need for GetComponent. We make it private so that it can’t be re-assigned. To make it accessible we create a public property.

We then null check before returning the instance. In the Awake method we assign the instance to the (this) script. Finally we create a public HasCard property of type bool.

Next we open the script for when the Player captures the card in the triggered cutscene. Now when the Player gets the card, we then make a call to the GameManager instance to set the HasCard property to true.

Now we can set up the OnTriggerEnter on the final scene trigger point and use this HasCard property as a conditional on whether to trigger the final cutscene.





Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Building Microservices with Azure Kubernetes Service and Azure DevOps — Part 3

Container Software Orchestration with Docker

How you can make your own command in Linux

Using Only Fiverr Resources to Build My First App

Becoming “Pro” on GitHub

The one thing that will make your start up more efficient

16 Reasons Why Only Your Users Do Not Read User Documentation

mac pod and ruby installation

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
Simon Leen

Simon Leen


More from Medium

Adding a thruster speed boost on keypress

Script Communication in Unity Using GetComponent<>

IDamageable Implementation!

Quick tip — Upgrading your Unity project to Universal Render Pipeline (URP)