Project 1: Space Shooter

How to load scenes in Unity

Adding main menu and a restart option

Eero Saarinen
3 min readJun 8, 2022
From the main menu to the game all the way till the end and then a restart.

Now comes the time to add two last UI elements to my game, at least for now they are the last. Namely the main menu that is displayed at the beginning of the game and a method to restart the game if the player so chooses.

So first I make a new scene for my main menu, either select it from the File menu or press ctrl+n, and build it as I see fit. Once it looks like I want it to look I add a new UI element called Button.

When I added the first UI element for my scene Unity automatically created an EventSystem for me. This allows me to map a method called LoadGame from my MainMenu script to the Button I created. The script contains only the LoadGame method, and it only loads the actual Game scene.

Linking the LoadGame method to the Button.

To load the Game scene, first it has to be added into the Build. This can be done by either selecting the Build Settings from the File menu, or by pressing ctrl+shift+b. Once the settings are open, the scenes in the Build can be dragged and dropped into a correct order. For me the order is first the main menu and then the game.

Adding scenes to the build, so that they can be referenced by their number.

This also gives the scenes a number, 0 for the main menu and 1 for the game. These numbers can be used with a method called SceneManager.LoadScene, to be able to use this function the UnityEngine.SceneManagement has to be added as well at the top with the using directive.

MainMenu script which loads the Game scene once the new game button has been pressed.

The method SceneManager.LoadScene does the actual loading, so using the 1 as an argument for it in the MainMenu script, I can load the Game scene once the player presses the button.

This exact same method is used in the GameManager script which, at the moment only, handles the restart of the game. The restart can happen only if the game is over and the player presses the R key, which in turn loads the Game scene again.

The GameManager script with the option to restart the game.

The UIManager script’s method GameOverSequence is updated to show the tip for the player on how to restart the game and to indicate for the GameManager that the game is over.

The UIManager script’s updated GameOverSequence method.

--

--