#10 Change Scenes at runtime in Unity. LoadScene Method

Let’s begin with a video on this topic:


Introduction

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.

Go to the project’s main page

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.

GO TO THE SOLUTION – FADE IN/OUT EFFECT FOR UNITY

Fade In – Fade Out Effect for Unity
👉

PLEASE CONSIDER SUBSCRIBING TO MY CHANNEL

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.

Fig. 1: Front view of LoadScene station.
Fig. 2: Inside the well of the LoadScene station.
Fig. 3: Inside the well of the LoadScene station.

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.

game object calling function to load a scene in unity
Fig. 4: At the bottom there is a capsule with Collider in trigger mode.

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.

hierarchy of a project in unity
Fig. 5: Project hierarchy.
gameobject components, the loadscene script is used to load a scene into unity
Fig. 6: LoadScene station components.

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.

blank script to complete
Fig. 7: LoadScene script to complete.

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.

two scenes from a project in unity
Fig. 8: Scenes from the project, GameDevLab and GameDevDimension.

Solution

To start with we have to import the namespace UnityEngine.SceneManagement

Fig. 9: Imported libraries at the top of the script.

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.

Fig. 10: Method to change the scene in Unity.

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.

compilation configuration window in unity
Fig. 11: Scenes are added in Build Settings.

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.

Fig. 12: Entering game mode and falling into the pit loads the GameDevDimension scene.


Fig. 13: Tunnel view of the GameDevDimension scene.

Fig. 14: Tunnel view of the GameDevDimension scene.

Fig. 15: Tunnel view of the GameDevDimension scene.

Conclusion

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.

Scroll to Top
Secured By miniOrange