Using a Unity button to switch between scenes

Unity allows to create multiple scenes but how do you switch between them with a script? In this tutorial you will learn how to create two scenes and switch between them by clicking a button

goto scene

Create two scenes

Your Project window should look like this:

project view

Change script to open scenes

Your code should look like this:

using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneSwitcher : MonoBehaviour
{
    public void GotoMainScene()
    {
        SceneManager.LoadScene("main");
    }

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

Create buttons to trigger the script

Repeat creating a button to go to menu scene

Written by Loek van den Ouweland on October 12, 2017. Questions regarding this artice? You can send them to the address below.
By using this site, you acknowledge that you have read and understand our Cookie and Privacy Policy.