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.