Unity: Switch between scenes

Difficulty: ★☆☆☆☆
Supported Unity version: Unity 5 or higher

Introduction

Prerequisites

  1. Two scenes: the tutorial below will guide you in creating the scenes
  2. Basic knowledge of C# programming

Steps

Step 1: Create a project / Launch your project

Step 2: (optional) change your scene name to TitleScene

To change the scene name, locate your Scene file in Project panel (by default it’s in the Scenes folder). Right-click and choose Rename. You will be asked whether you want to reload the scene, select Reload.

Select “Rename” to change the scene name
Click Reload

Step 2: Create a User Interface (UI) element: Button

To create a “Start Game” button, add a UI button Game object via Unity menu > GameObject > UI > Button.

Add a UI button into the scene

After adding the button, there will be a few new elements appearing in the Hierarchy panel, including Canvas, Button and EventSystem. Do not remove them.

The Scene panel is now confusing, as the Button is huge, compared with the original camera area. If you zoom out in the Scene panel, it will look like this:

The original camera area is at the lower-left corner (the tiny little rectangle)

The big rectangle is the Canvas, which is the area for displaying the user interface elements. To make the Canvas size aligned with the camera, we can change the settings of the Canvas via the Inspector panel.

Select Canvas in the Hierarchy window, then in the Inspector panel > Canvas > Render Mode, choose “Screen Space -Camera”, and select the Render Camera as “Main Camera” in the scene. As a result, the Scene panel will become:

Finally aligned with the Camera area

Step 3: (optional) Resize and Move button to a better position

The button is now at the lower right corner, which is undesirable. We shall move it to the centre of the scene. The text of the button can be edited via the Text GameObject under the Button in the Hierarchy window.

Optionally you can add your game title as a GameObject > UI > Text.

Once finished updating the UI elements, hit the Play button to start the game. It will look like this:

Looks basic now

Click again the Play button to stop the Play mode.

Remember to save the changes.

Step 4: Create another Scene, named GameScene

To create a new scene, go to Unity menu > File > New Scene. Save the Scene as “GameScene” and save it in the Scenes folder.

In the newly created GameScene, create a text UI element to indicate it’s the game scene (remember to change the Render Mode of Canvas):

Something like this

Save the changes.

Step 5: Create the button action in TitleScene

Go back to TitleScene, select the Main Camera. In the Inspector panel, select “Add Component” and create a new C# script named TitleSceneController.(note the capital letters and no space please). Double click the script to launch Visual Studio editor (in the older versions, Mono Editor will be launched instead).

The script will look like this by default:

The default template for a Unity C# script

We will first add a line around line 4, to add a required namespace to the script:

using UnityEngine.SceneManagement;

Then, we will add a piece of code before the last closing curly bracket:

public void ClickStart() {
SceneManager.LoadScene("GameScene");
}

The completed code is as follow (the codes highlighted in bold are newly added):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class TitleSceneController : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

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

}
public void ClickStart()
{
SceneManager.LoadScene("GameScene");
}

}

Explanation:

We added a function called ClickStart() , which is used to tell the button what to do when the “Press Start” button is clicked.

Step 6: Assign the function to the Start button

Back to Unity editor, select the Button. In the Inspector panel, there is an On Click section. Click the “+” sign and select the Main Camera in scene as the object. Then in the No Function pull down menu, select TitleSceneController > ClickStart().

Select the Main Camera as the object, then select ClickStart()

Save the changes.

Step 7: Configure Build Settings to add all related scenes

Go to Unity menu > File > Build Settings, we drag all the scenes from the Scenes folder in Project panel to the upper part of the Build Settings dialog.

You can see all 2 scenes have been added to the Build Settings dialog

Close the Build Settings dialog afterwards.

If you have more than two scenes, make sure you add all of them into the Build Settings dialog.

Done!

From this point, the game is ready. Click Play button and try to click the “Start Game” button. It will redirect to Game Scene.

If you encounter any questions, please leave a comment below. I’ll be happy to help.

Completed Project

--

--

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
Raptor Kwok

I write stuffs: novels, programs, mobile apps, journal papers, book chapters, etc.