Loading Scenes in Unity

Objective: Easily loading a scene pressing a button in our Main Menu.

Bonus: Reloading active scene. Exit the application.

Requisites: 2 different scenes, 3 button Canvas setup and a Main Menu script.

One thing every game needs it’s a functional Main Menu. In this tutorial we are going to explore how to load a scene, reload scene and exit our application by using Unity Events. Let’s get into it!

Right click in the hierarchy and create a UI -> Button. A Canvas will appear, create two more buttons inside the Canvas. Let’s give them some fancy names

Ignore the Title_Screen_img, that’s just the img for my game.

Time to create our Main Menu script, open it and on the top type the namespace using UnityEngine.SceneManagement. We will need this library to load the scenes.

SceneManagement namespace essential to load scenes

Now we will need three different public methods, one for each button that will have their own functionality.

First: LoadGame() will allow us to load the game on a single click. Debug.Log just added to confirm the method is being called.

SceneManager.LoadScene(1). One is the position in our build index. In the Unity Editor: File - Build Settings we have to add both scenes to the “Scenes in Build” to make them loadable.

Second: ReloadScene() will store the active scene info and load it by name. Debug.Log just added to confirm the method is being called.

Third: QuitGame() will simply exit the Application. Debug.Log just added to confirm the method is being called.

After checking everything is fine, save and go back into the Unity Editor. Drag the Main Menu script to the Canvas.

Time to give some nice functionality to our buttons. Let’s select our NewGame_Button, press the + in the Onclick() in the button component and drag the Canvas Gameobject. Select our LoadGame() method and that will be it for this button.

NewGame button setup

Repeat the same process for every button selecting ReloadScene() or ExitGame().

With everything on it’s place, its testing time.

Easy button setup

Press every button to check it’s functionality.

Debug messages to check

If everything is correct, you can delete the Debug.Log messages on the Main Menu script and that would be it for this tutorial.

Passionate Unity developer always looking for new challenges!