Loading Scenes in Unity
This time, we will be looking at how to restart our current scene and create a main menu. First, we have to create a GameManager component to run our logic through. We have it as a separate component so that we have a central location to run logic through that has no connection to any of our current scripts.
From here, we can go into our GameManager script and set up our code for GameOver. We have to make sure that our script is using UnityEngine.SceneManagement. From here, we can set up our R key as the restart key, and also have it so that it can only be pressed when the game is over.
As for the LoadScene(0), we have to setup our scene within the editor:
Once we have that set up, we can go over to our game manager script and add in our GameOver() code to the GameOverSequence void we have created. This was made so that we can clean up our if statement and if we have any errors, it is easier to look for it through the void:
Finally, we can run our game and see if it all works as intended:
Now that we have a working restart button, we can look into creating a new Main Menu scene to work with. First, let’s make ourselves a new scene:
Once we have our new scene made, we can save it in our scenes folder as Main Menu. From here, we can create a UI image component and drag in the Main menu image that we have to work with:
Once we have it in place, we can now adjust the size to what it is designated at, which in this case is 512x512. As well, we can change our main camera to a solid colour and black:
From here, let’s add in the spaceBG that we have with our main game, as it is more visually appealing than the solid black background, along with resizing the image so that it fits nicely within our play area:
Now that we have our main menu screen looking like what we want, let’s add a button that the player can interact with the start a new game:
Now that we have the button set up, we can rename it and adjust the colours around to whatever we want. With Unity, we can change a lot of things with how we interact with our button, but for now we will just have it change colour on click:
Next, we are going to adjust our scene numbers in the build settings so that when we load the game up, it starts on our main menu:
As we have changed the number of our game screen, we have to remember to go back into our game manager script and change the number for our reset button press:
Next, we need to have the button have a function rather than a colour change on press. We are going to create a new folder called “Main Menu” under scripts so that we can keep everything that is related to the Main Menu in that folder. Next, we will create a main menu script and attach it to our canvas.
In our newly made script, we have to add in our UnityEngine.SceneManagement at the top, then we can create a new public void:
We need to be sure to name it Public so that we can attach it to our onclick event within the button, as such:
Now that we have it all setup, we can check to see how it works:
Now that we have a functioning main menu, it’s time to go back into our game and add a few VFX along with a bit more polish around the edges.