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.
Given below is the complete guide on how to set up and manage multiple scenes in Unity.
Go to File > new scene
Add a new 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 theOnCollisionEnter
method for this example but we will useOnTriggerEnter
method instead.Whenever the object (tagged as
Player
) collides with thisGameObject
, the scene will be changed to the next scene specified in the C# script.
Create a new scene
Set up this scene according to your requirements
Note: Make sure to save the scenes after setting up.
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 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);}}}
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.
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.
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.
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.