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

Philip Johnson
3 min readJun 24, 2021

--

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.

We create the game manager object in the hierarchy as well as a GameManager script. WHen you create this script unity will represent it with a gear icon.

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.

We will hook the game manager up to the UI manager script by finding the game object of the same name once the game starts

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 UI manager script will call the public method in the game manager to reset the game

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.

This simple main menu script will give functionality to the button. We attach this script to the canvas object in the main menu scene.

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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Philip Johnson
Philip Johnson

No responses yet

Write a response