#10 Change Scenes at runtime in Unity. LoadScene Method
This is the last exercise in Unity’s Fundamental Series, in this article we’re going to see how to change the scene in Unity at runtime.
Understanding how to change the scene is important because it allows us to separate the content of our game into parts, for example the main menu on one side and the game itself on the other. Another example could be a village with different buildings in which you can enter, the village can be built in a scene and then use multiple scenes for each building, or use a scene for all buildings and choose the appropriate one at the time of loading the scene.
Before we start I invite you to watch the video I made to summarize this article. English Subtitles available.
Maybe you find it useful…
I invite you to see this article and its respective video on how to achieve a Fade In / Fade Out effect in Unity, you can apply this to what we see in this article to achieve a change of scene not abrupt.
Scenes in Unity
Each scene in Unity contains its own hierarchy of GameObjects, depending on our needs we can use them in different ways.
Some simple examples of using scenes are using one scene for the menu and another for the game, each scene is a level of our game, do everything in a single scene, etc..
In our projects we probably have to make changes of scene, that’s why I consider it part of one of the fundamental things of Unity.
GameDevLab LoadScene Station
To learn how to change a scene in Unity we’re going to use GameDevLab’s LoadScene station, which consists of a strange well that looks like it disintegrates matter and sends it through space-time.
At the bottom of the well there is a capsule (figure 4) that has a Collider assigned in trigger mode and a Script that will detect the character and send a message to the Script that we have to complete.
The complete station is contained in the GameObject “#10 LoadScene” of the hierarchy, this GameObject is assigned the Script “LoadScene” which is the Script we have to complete in this exercise.
The Script to complete contains a single public method called “teleport”, this method will be executed by the capsule Script when the character is detected at the bottom of the well.
If it is not clear what a method is in my channel there is a video in which I talk about methods in programming (english subtitles available) and there is also an article in the programming section of this page.
What we are going to do is change the scene within the method “teleport”, for this we go to the folder scenes of the pack of the fundamental series and we see that there are two scenes (figure 8), one is the GameDevLab and the second scene is GameDevDimension, the latter is the one we are going to load using its name.
To start with we have to import the namespace UnityEngine.SceneManagement
Then to change the scene in Unity we execute a static method of the “SceneManager” class, the LoadScene method, to which we pass as parameter a String with the name of the scene we want to load.
With that simple instruction we can make the scene change, but for this to work all the scenes have to be added to the compilation, this we do in File > Build Settings, opening each scene and clicking Add Open Scenes.
When the character falls into the pit a new scene is loaded in which the camera automatically advances and the controls are disabled, when the camera reaches the end of the tunnel the GameDevLab scene is automatically loaded again.
To change a scene in Unity we need to import the UnityEngine SceneManagement library and use a static method of the SceneManager class.
We can refer to a scene by its name or by its identification code that appears in Build Settings, in this case we use the name.
Loading a new scene is a relatively simple task, the problem is that the new scene is predefined and always starts from scratch.
For example a scene can be a house with ten coins, the character enters, takes the coins and leaves the house, ie returns to the previous scene. If we do nothing about it, when we re-enter the house scene the ten coins will be inside again.
To prevent this we have to store information of the state of the variables in the scene and read this information at the beginning of the scene.