How to manage multiple scenes in Unity

scene in Unity can be considered a separate level in a game that can be linked to other levels before or after it. Managing multiple scenes in Unity is essential, allowing the game to load the assets per scene. Thus, it provides a structured effect to your games or simulations. 

How to manage scenes

Given below is the complete guide on how to set up and manage multiple scenes in Unity.

Create a scene

  • Go to File > new scene

  • Add a new scene

Set up the scene

  • Add different GameObjects or set up the scene according to your project requirements

Note: For this answer, we will change the scene when the player collides with another GameObject inside the scene.

  • Make sure to set the Is Trigger property of at least one of the GameObjects to true. We can also use the OnCollisionEnter method for this example but we will use OnTriggerEnter method instead.

  • Whenever the object (tagged as Player) collides with this GameObject, the scene will be changed to the next scene specified in the C# script.

Add second scene

  • Go to the scenes folder (or create on if it doesn't exist already)

  • Create a new scene

  • Set up this scene according to your requirements

Note: Make sure to save the scenes after setting up.

Configure build settings

  • Go to File > build settings

  • Drag and drop all the scenes here

  • And check the checkbox to include them in the final build.

Note: The order in which scenes are added in the build settings does not matter.

Create a C# script

  • Create a new C# script to transition between scenes

A sample C# script is given below.

using UnityEngine;
using UnityEngine.SceneManagement;
public class changeScene : MonoBehaviour
{
public string sceneToLoad;
public void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Player")
{
SceneManager.LoadScene(sceneToLoad);
}
}
}

Explanation

  • Lines 1–2: These lines make all the necessary imports.

  • Line 4: A class named changeScene is declared here which inherits from the MonoBehaviour class.

  • Line 6: It declares a variable named sceneToLoad to load next scenes.

  • Line 8–12: Triggers can be used in Unity to change the game levels. Here, the OnTriggerEnter method is used to invoke the LoadScene method that changes the scene. So, it will trigger a scene change whenever the player enters the trigger area.

Note:

  • You can set multiple triggers in your game as well.

  • Make sure to set the tag for the player to Player which helps to identify the player. As other objects, when they collide with the trigger might change the scene as well.

Assign the C# script to GameObject

  • Assign the C# to the GameObject which has Is Trigger property set to true. This changes the scene as soon as the player collides with it.

Set the scene name

  • Select the GameObject on which the "Is Trigger" property is set to true

  • In the Inspector window, set the scene name in the sceneToLoad section to the scene which you want to load.

Note: Make sure that the scene name entered in the sceneToLoad section matches exactly with the name of the scene you want to change to.

Demonstration

Create second scene
1 of 5

Sample project

Explanation

In the above project, there are two different scenes set up. Whenever the player collides with the rectangle ("Is Trigger" property set to true), the C# script is called. The game then changes into the new scene: the scene specified in the sceneToLoad section.

Continue reading about Unity

Copyright ©2024 Educative, Inc. All rights reserved