What’s on the menu!?

Loading Scenes in Unity

Now that we have a game over screen let’s give our players a main menu screen when they first load the game! To start let’s create a new scene in our scenes folder in the project window and name it Main_Menu.

Our new Scene

Now that we have our new scene we can add a canvas. This also adds an EventSystem object that we will need to upgrade because we are using the new input system. This is as easy as clicking on the EventSystem in our hierarchy to load its properties into the inspector and clicking the button to upgrade our system. This will also sometimes require us to restart Unity.

Now that we have our canvas let’s add our main menu graphic, our background graphic, some text for credits, and a button for the user to push. Let’s also change the skybox setting on our main camera object to solid color and set the color to black.

Main Menu Screen

Looking good! Let’s create an empty object in our hierarchy and call it Main_Menu_Manager and then attach a new C# script to it called MainMenu (or MainMenuManager if you prefer, I’m going with the shorter version).

Main Menu Script

We don’t need our start or update functions right now so let’s clean them up early but deleting them and then adding our new StartGame() method to house the functionality that kicks off when the player clicks the start game button, namely loading the Game scene.

MainMenu Script

To load our Game scene, we need to use the LoadScene method of the SceneManager class and pass in the build index of our Game scene. We can pass in the value 1, but we know from our previous article that we do not want to use a magic number.

We may also be tempted to use the SceneManager.GetSceneByName(string sceneName) method that takes in a scene name, but the caveat for this version of this method is that the scene has to already have been loaded into the running game. If we try to use this version without preloading the Game scene we will get back NULL as our value, breaking our game.

In our case we are going to make the assumption for now that the Main_Menu scene is loaded as the first scene in the build settings window and that the Game scene follows it. This means we can use the SceneManager.GetActiveScene().buildIndex property to get our Main_Menu scene index and add 1 to it to get our Game scene buildIndex.

Let’s not forget to add our scenes to our build windows in the correct order!

Proper build settings for our game

Now that we have our function in place, we can assign it to the click handler for our button back in Unity.

Assigning the Click Event

All that is left is to adjust our game restart code to make sure it uses the right build index to restart our game. Let’s save our Main_Menu scene and open our GameManager script where our reload code is and ensure that we are calling the OnRestartLevel method with SceneManager.GetActiveScene().buildIndex.

OnRestartLevel Method in GameManager

Next Time!

Now we can run our game with the main Menu scene open and click into our game! Next time we will look at starting to add visual effects by making our enemies go boom! If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.

Unity Game Developer, Software Engineer, Gamer, Musician, and Father. Christopher is a creative that enjoys a challenge and loves coding.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store