0 of 16 lessons complete (0%)
Essential Activities

Code a button to load a different scene or close the application

You don’t have access to this lesson
Please register or sign in to access the course content.
Length: 30 minutes

This lesson build on from Creating a UI, make sure that you have completed that one first.

Open your Scene with a UI Menu

Create an empty object on the scene called MenuManager

Create a new Script called Menu Manager

Attach the script to the Menu Manager object.

Open the MenuManager script in the editor

We are going to add public methods to this to handle processing input from the buttons.

Delete the Start and Update methodss.

Add the code below.

Lines 4 and 5 import the UI and SceneManagement packages.

The public method DisplayCanvas on lines 10-13 takes one Canvas parameter[1], the canvas to switch[3] to.

Ideally we would be able to have the current one as a parameter but due to limitations with the Unity Editor we can only have one parameter.

We first need[4] to turn off all of the canvases on the scene by setting their activate status to false.

On Line 11 we find all of the loaded Canvas objects. Note that this will turn off all Canvas objects so it might not be the best solution for you. It actually won’t be in 90% of the cases, but it works.

Now we need to turn them off. This uses a for each loop[5] that loops through each Canvas in the list as shown on line 12. The list is closed on line 14.

Line 13 sets each Canvas object in the list to have it’s active status set to false.

Line 15 sets the one canvas that we want to change to to have an active status.

Lines 19-21 has a public method that will quit the application. This uses the Application.Quit() method. Note that this will not work inside the Unity editor, you need to have a Unity build for this to work.

Finally Lines 23-25 are similar to the load scene triggers previously covered here. The difference here is that we don’t use a trigger we run this method when the button has been pressed.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class MenuManager : MonoBehaviour
{

    public void DisplayCanvas(Canvas canvas) {
        Canvas[] canvases = FindObjectsOfType<Canvas>();
        foreach(Canvas c in canvases) {
            c.gameObject.SetActive(false);
        }
        canvas.gameObject.SetActive(true);

    }

    public void Quit() {
        Application.Quit(); // This will only work in a build not in the editor
    }

    public void LoadScene(string sceneName) {
        SceneManager.LoadScene(sceneName);
    }
}

Now we need to link these methods up to buttons.

First select the Play button on the Main Canvas.

Look in the inspector for OnClick()

Click the +

Drag the MenuManager Object onto where is shows None (Object)

This links the Menu Manager to the button

Now we need to link the method.

Select No Function[2] and then the name of your script and the name of the method to call.

In this case[6] LoadScene

Type the name of the Scene you want to load.

Setup the other buttons in a similar manner.

For the About button, add the DisplayCanvas method

Then add the about Canvas

For the Exit button add the Quit method.

Finally on the Back button on the About Canvas link the DisplayCanvas method to display the Main Canvas

Now you need to make sure that all of the Scenes have been added to the build.

Go to File > Build Settings

Make sure that all of the Scenes you are going to use are added.

The buttons should now all work.

Press the play button to test them. Note the Exit button will not work in the editor. You will need to create a build for that.

Terms definitions
2. Function. A named section of a computer program that performs a specific task. Functions help make code more efficient and reusable. They may take input parameters and produce output.
3. switch. A control[11] mechanism in which the value of a variable[8] or expression changes the execution of a program and which usually involves multiple branches.
4. need. An identified requirement of a person, group, or environment[9]. A need is identified from an issue[12] and sits within a context[13]. Technological practice[7] can be undertaken in an attempt to meet an identified need.
5. loop. A sequence of instructions in a program repeated until a certain condition[10] is reached: * with a “for loop”, the instructions are repeated a certain number of times * with a “while” loop, the instructions are repeated until a given condition becomes false * with a “forever loop” (also known as an “infinite” or “endless” […]
Terms definitions
1. parameter. Length: 30 minutes This lesson build on from Creating a UI, make sure that you have completed that one first. Open your Scene with a UI Menu Create an empty object on the scene called MenuManager Create a new Script called Menu Manager Attach the script to the Menu Manager object. Open the MenuManager script […]
2. Function. Length: 30 minutes This lesson build on from Creating a UI, make sure that you have completed that one first. Open your Scene with a UI Menu Create an empty object on the scene called MenuManager Create a new Script called Menu Manager Attach the script to the Menu Manager object. Open the MenuManager script […]
3. switch. Length: 30 minutes This lesson build on from Creating a UI, make sure that you have completed that one first. Open your Scene with a UI Menu Create an empty object on the scene called MenuManager Create a new Script called Menu Manager Attach the script to the Menu Manager object. Open the MenuManager script […]
4. need. Length: 30 minutes This lesson build on from Creating a UI, make sure that you have completed that one first. Open your Scene with a UI Menu Create an empty object on the scene called MenuManager Create a new Script called Menu Manager Attach the script to the Menu Manager object. Open the MenuManager script […]
5. loop. Length: 30 minutes This lesson build on from Creating a UI, make sure that you have completed that one first. Open your Scene with a UI Menu Create an empty object on the scene called MenuManager Create a new Script called Menu Manager Attach the script to the Menu Manager object. Open the MenuManager script […]