Day 10: Using Unity’s scene management system to load different scenes and setting up our game manager

Objective: To wrap up the UI portion of this simple shooting game so that we can move on to polishing the graphics with VFX, post processing and audio.
To wrap up the UI segment of this project we’ll set up the main menu scene that leads into the game loop as well as an option to reset the level and start over without having to go back to the main menu.

First we’ll create the functionality to reset the level when the player dies. What we don’t want to do is pass user input into the UI manager per say. It’s better to have that handled by a main game manager. We will create a game manager game object and a script named GameManager.

The game manager script’s purpose for right now will be to accept user input in the update loop when the game over is triggered by the UI manager. We’ll add a hook to the game manager inside of the UI manager. at the start of the game we will use GameObject.FInd(“object name”) to locate the game manager and assign it to the variable _gm.

The game manager itself is simple in that it has a bool for the game over state and a public method that will trigger this bool to be true. If the game over state has been triggered then the player can hit the ‘“R” key to start the level over again.

To reload the level we will use Unity’s scene management system to load different scenes in this project. In order to use this library we need to have the using UnityEngine.SceneManagement directive at the top. In order for us to load the scene of our choosing you have to go into the BUILD settings and add the scenes. Each scene will be assigned an index and you’ll use this int value to load the desired scene.
Now that we have that down we can build the main menu scene for the starting point of the game. We’ll create a new scene and call it MainMenu. We’ll create the UI objects we need to design the look of this scene. The most significant thing here is the button object. The button object has different states such as when a mouse pointer is hovering over it, when its pressed and when it’s idle. We will create a MainManager script to handle the button because the UIManager script is rigged up to take in scripted objects that have to exist for the game to run and in this scene we don’t need to much going on.

After we have the title screen set up we will add this script to the UI canvas object. We will select the button and drop/drag the canvas object in the hierarchy into the OnClick events field so that we can call the MainMenu script’s public method to load the game scene.
