Skip to main content

Creating a Menu

There is a tutorial on Unity covering this.

Create a new blank Scene

Save the Scene

Add a Game Object UI Button

A button will be added to the Scene

Move it around as you would any object.

Modify the button as you want

Changing the Text is hidden away

In the hierarchy panel open the Button object and edit the Text Object inside it.

Open the Button panel in the inspector

You can customise the button here further

Drag in the game object that you would like to have the button modify

You can select from all of the assets in the program. Or a Script that has a function to run

We now need to create a GameController, this will manage the game logic.

Create a new empty GameObject and rename it GameController

Drag the GameController object into the Project panel

Create a new Script called ButtonManager.

Add the code below

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

public class ButtonManager : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}

public void GotoSampleScene()
{
SceneManager.LoadScene("SampleScene");
}

public void GotoMenuScene()
{
SceneManager.LoadScene("Menu");
}
}

We need to import the Unity SceneManager. This is done with the line using UnityEngine.SceneManager;

We create two functions to switch between the scenes.

This code creates a function called GotoSampleScene. When it is called the SceneManager uses the LoadScene method and will load the scene named SampleScene

Note that the argument given to the LoadScene() method takes a string argument.

This argument is the exact name of the scene.

Add the ButtonManager script to the GameController object.

Now we need to add the script code to the button

Using the OnClick property in the Inspector add the ButtonManager script

Change No Function to the function / method you want to call.

You will see a list of the options.

Select ButtonManager and then the appropriate method.

In this case GotoSampleScene()

Run the program.

We will see an error

This is because we haven't added the Scene to the build settings for the project.

Open the Sample Scene.

Go to File and select Build Settings...

Click Add Open Scenes

Close the Build Settings

Repeat this for any other Scenes

You can now create a button and switch between different scenes.