Restarting the game and making the Main Menu

Today’s article is about restarting the game when the game is over and creating the main menu.

First, open your project and right-click on your ‘Canvas’ game object. Create a new text object and rename it to ‘Restart_Text’. We will anchor the object to the bottom center of the scene. We will also write in the text box ‘Press the ‘R’ key to restart the level.’ We can change the color to white, the font size to 15. Then we can set both the ‘Horizontal’ and ‘Vertical Overflow’ to ‘Overflow’. Go ahead and turn the game object off like the ‘Game_Over_Text’ object.

We will also create an empty game object and name it ‘Game_Manager’. We will create a new script also and name it ‘GameManager’ and attach the script to the game manager game object.

Let’s open up our ‘UIManager’ script and create a couple of variables. First, a serialized private text variable called ‘_restartLevelText’ and the second a private GameManager handle named ‘_gameManager’. In ‘Start’ we need the SetActive of our ‘_restartLevelText’ to be false, just like the ‘_gameOverText’. Then we need to use GameObject.Find for the ‘_gameManager’. After that is done, we need to null check and make sure the game manager is active. Our ‘Start’ function now looks like this.

Next, we’re going to create a new function for our game over stuff so it doesn’t clutter up the ‘UpdateLives’ function. Let’s name our new function ‘GameOverSequence’ and take the lines of code about our ‘_gameOverText’ and our coroutine and put them in here. Now in the ‘currentLives ==0’ statement, we can call our ‘GameOverSequence’. After our ’_gameOverText’ line of code, we can change the SetActive of our ‘_restartLevelText’ to true as well.

We have to work on our ‘GameManager’ script now and create a serialized private bool named ‘_isGameOver’. We will also need to add a new namespace to the top of the script, ‘using UnityEngine.SceneManagement’. We will create a new function called ‘GameOver’ and set our bool to true. In ‘Update’ we will be checking our input and if we press ‘R’ and ‘_isGameOver’ is true, then we will type in ‘SceneManager.LoadScene(0)’. This line of code will load whatever scene zero is in our build settings, which we will have to add in a moment. Here is the GameManager script in its entirety.

In Unity go to ‘File’, then ‘Build Settings’ and drag your scene into the box at the top.

Now, make sure your ‘Restart_Text’ game object is placed in its slot on the ‘UIManager’ component on the ‘Canvas’ object. Let’s test out our restart.

Now to create our main menu. We’re going to create a new fresh scene. Once you did that right-click in your hierarchy and create a new image through the ‘UI’ dropdown. Rename that as ‘Title_Screen_img’ and place the sprite you want on your background on the ‘Source Image’ slot. You can add multiple images to create the title screen you wish. Checkmark the ‘Preserve Aspect’ box. You can also change the background to a solid color under the ‘Main Camera’ settings. Next, we’re going to create a button to press to start the game. While canvas is selected, right-click and select ‘Button’ under the ‘UI’ dropdown. Position the button where you wish and anchor it in that location.

Next, we’re going to create a new ‘MainMenu’ script and add the SceneManagement namespace. We’re going to create a new function called ‘LoadGame’ and use the LoadScene method of SceneManager. This time we’re going to call scene 1. This is because Main Menu needs to be scene 0 now that we have the main menu. Scene 1 will be the next scene to load in the series. This will also need to be changed in the ‘GameManager’ script.

Main Menu code
GameManager scene changed from 0 to 1

In Unity, click on your button and click the ‘+’ icon under ‘On Click{}’. Drag the ‘Canvas’ object to the slot and through the dropdown choose ‘MainMenu’ and then the ‘LoadScene’ function. When this button is pressed now, the next scene will be loaded. But first in your build settings drag the ‘MainMenu’ scene to the top of the ‘Scenes in Build’ box. Save and play.

Everything should be working great and we will continue on in the next article.