Getting Started with Unity (Part 8: Loading Scenes & Creating a Main Menu)
Each unique interface in Unity — from the main menu to the loading screen, and all of the levels — is called a scene. Navigating between scenes is a core principle to understand to bring your games to life, so let’s take a look at Unity Scene Management.
The first way we can use Unity scene management in our Space Shooter game is to restart the level after the player has died. Let’s first create a text UI element to let the user know they have that option:
We assign our text element to the restartText variable, and then update the player death method to set the restart text active when the game ends.
Great. Now let’s make it work. We only want the (R) key to restart the level if the game is over, so let’s create a bool check to be sure this is the case. We create a gameOver = false bool, and set it to trigger true when the restart level menu is on screen:
Then we check for user input in our Update() method while confirming gameOver = true:
Now for us to be able to engage the Scene Management system, we need to add the associated Library by including:
at the top of the script. Since we will be needing to use this script in multiple scenes for tasks unrelated to our current UI manager, let’s create a new GameManager script and handle scene management from there:
With the Scene Management library added, we can now use SceneManager commands, namely the LoadScene() command. This lets you specify which scene to load based on name, index position, or position relative to the current scene (next, last, etc.).
Currently we only have one scene in our build index, so it is referenced with BuildIndex position 0. When we create a main menu we will need to update this position to 1 to reload from the beginning of the game rather than the main menu. The Main Menu will then become build index position 0 which loads when the game first boots up.
Now that our game manager is created, we can create a handle to it from our UIManager and call the ReloadScene() method if the game is over and the user presses R:
Be sure to navigate to File -> Build Settings and add the current scene to the build for this code to properly execute.
Our scene reload is working properly! We’ll add some effects later to make it more clear that the level has been refreshed upon restart.
Now let’s create a main menu. Save the scene and navigate to file -> new scene. Save the scene as MainMenu in the Scenes folder and we’re ready to begin.
We have an image asset for our title screen which we can implement by creating a new object -> UI -> Image and dragging the asset into the source image element.
Be sure to select preserve aspect and then we can update the dimensions to the correct size — 512x512.
Let’s darken our background to create a consistent vibe with our game. Select the main camera, update SkyBox to Solid Color in the clear flags and change the background color to black.
Next we can drag our space nebula background asset into the hierarchy and resize it to fit our screenspace, to keep a consistent look with the game.
Looking great already! Let’s add a button to start our game.
Right click canvas -> UI -> Button:
The button element has a number of features you can edit, including the text and background color. I’ve spiced mine up a bit with a background color and hover color and also added another couple of text fields for directions and developer credit:
Don’t forget to anchor your text and image elements and set the canvas scaler to scale with screen size to be sure your menu looks good on all screens.
We’re ready to make our button functional! Let’s add our GameManager script to the Canvas game object and open it up to create a method for loading the next scene. Because we only have one game scene we can actually use the same method we are using to restart the game if the player dies. Let’s rename this method in both the GameManager and the UIManager reference to call it LoadGame()
Now back on our button game object, inside the button component there is a section for OnClick (). OnClick() allows you to set a method to execute from any script on any object when a button is clicked.
In our case, we want to drag the Canvas game object into the On Click element box currently showing “None (Object)” because the Canvas contains the GameManager script which contains the LoadGame() method we wish to call when this button is clicked.
You can browse the options menu to locate the appropriate script and method within that script.
Now our button should be all set up to load our game!
We’re all set! We have a spiffy new interactive menu that leads into our playable game and users can easily restart the game if their player dies. Woohoo!
Now we can spice up our game with some visual effects. I’ll cover that in the next guide. See ya there!