How to manage multiple scenes in Unity
A 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
GameObjectsor set up the scene according to your project requirements
Note: For this answer, we will change the scene when the player collides with another
GameObjectinside the scene.
Make sure to set the Is Trigger property of at least one of the
GameObjectsto true. We can also use theOnCollisionEntermethod for this example but we will useOnTriggerEntermethod instead.Whenever the object (tagged as
Player) collides with thisGameObject, the scene will be changed to the next scene specified in the C# script.
Add second scene
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
sceneToLoadto load next scenes.Line 8–12: Triggers can be used in Unity to change the game levels. Here, the
OnTriggerEntermethod is used to invoke theLoadScenemethod 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
Playerwhich 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
GameObjectwhich 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
GameObjecton which the "Is Trigger" property is set to trueIn the Inspector window, set the scene name in the
sceneToLoadsection to the scene which you want to load.
Note: Make sure that the scene name entered in the
sceneToLoadsection matches exactly with the name of the scene you want to change to.
Demonstration
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
Free Resources