#10 Change Scenes at runtime in Unity. LoadScene Method

IMPORTANT UPDATE FOR THIS ARTICLE

This arti¬≠cle is part of an old¬≠er video series, I have cre¬≠at¬≠ed a use¬≠ful solu¬≠tion that allows you to load any scene with a but¬≠ton just know¬≠ing the name of the scene and you can reuse this solu¬≠tion just drag¬≠ging a script to a but¬≠ton and chang¬≠ing the name of the scene you want to load.

Go to the new solution

Here I explain how this solu¬≠tion works:

How to LOAD Scenes with name in UNITY
ūüĎČ
(DOWNLOAD FILE BELOW)

PLEASE CONSIDER SUBSCRIBING TO MY CHANNEL

CHECK OUT THIS CRAZY EXPERIMENT

OLD ARTICLE CONTINUES BELOW

Introduction

This is the last exer­cise in Unity's Fun­da­men­tal Series, in this arti­cle we're going to see how to change the scene in Uni­ty at runtime.

Under¬≠stand¬≠ing how to change the scene is impor¬≠tant because it allows us to sep¬≠a¬≠rate the con¬≠tent of our game into parts, for exam¬≠ple the main menu on one side and the game itself on the oth¬≠er. Anoth¬≠er exam¬≠ple could be a vil¬≠lage with dif¬≠fer¬≠ent build¬≠ings in which you can enter, the vil¬≠lage can be built in a scene and then use mul¬≠ti¬≠ple scenes for each build¬≠ing, or use a scene for all build¬≠ings and choose the appro¬≠pri¬≠ate one at the time of load¬≠ing the scene.

Go to the project‚Äôs main page

Before we start I invite you to watch the video I made to sum­ma­rize this arti­cle. Eng­lish Sub­ti­tles available.

Maybe you find it useful…

I invite you to see this arti­cle and its respec­tive video on how to achieve a Fade In / Fade Out effect in Uni­ty, you can apply this to what we see in this arti­cle 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 Uni¬≠ty con¬≠tains its own hier¬≠ar¬≠chy of GameOb¬≠jects, depend¬≠ing on our needs we can use them in dif¬≠fer¬≠ent ways.

Some sim­ple exam­ples of using scenes are using one scene for the menu and anoth­er for the game, each scene is a lev­el of our game, do every­thing in a sin­gle scene, etc..

In our projects we prob¬≠a¬≠bly have to make changes of scene, that's why I con¬≠sid¬≠er it part of one of the fun¬≠da¬≠men¬≠tal things of Unity.

GameDevLab LoadScene Station

To learn how to change a scene in Uni­ty we're going to use GameDevLab's Load­Scene sta­tion, which con­sists of a strange well that looks like it dis­in­te­grates mat­ter and sends it through space-time.

Fig. 1: Front view of Load­Scene station.
Fig. 2: Inside the well of the Load¬≠Scene station. 
Fig. 3: Inside the well of the Load¬≠Scene station. 

At the bot­tom of the well there is a cap­sule (fig­ure 4) that has a Col­lid­er assigned in trig­ger mode and a Script that will detect the char­ac­ter and send a mes­sage to the Script that we have to complete.

game object calling function to load a scene in unity
Fig. 4: At the bot¬≠tom there is a cap¬≠sule with Col¬≠lid¬≠er in trig¬≠ger mode.

The com­plete sta­tion is con­tained in the GameOb­ject "#10 Load­Scene" of the hier­ar­chy, this GameOb­ject is assigned the Script "Load­Scene" which is the Script we have to com­plete 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: Load­Scene sta­tion components.

The Script to com¬≠plete con¬≠tains a sin¬≠gle pub¬≠lic method called "tele¬≠port", this method will be exe¬≠cut¬≠ed by the cap¬≠sule Script when the char¬≠ac¬≠ter is detect¬≠ed at the bot¬≠tom of the well.

If it is not clear what a method is in my chan­nel there is a video in which I talk about meth­ods in pro­gram­ming (eng­lish sub­ti­tles avail­able) and there is also an arti­cle in the pro­gram­ming sec­tion of this page.

blank script to complete
Fig. 7: Load­Scene script to complete.

What we are going to do is change the scene with¬≠in the method "tele¬≠port", for this we go to the fold¬≠er scenes of the pack of the fun¬≠da¬≠men¬≠tal series and we see that there are two scenes (fig¬≠ure 8), one is the GameDe¬≠vLab and the sec¬≠ond scene is GameDe¬≠vDi¬≠men¬≠sion, the lat¬≠ter is the one we are going to load using its name.

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

Solution

To start with we have to import the name­space UnityEngine.SceneManagement

Fig. 9: Import¬≠ed libraries at the top of the script. 

Then to change the scene in Uni¬≠ty we exe¬≠cute a sta¬≠t¬≠ic method of the "Scene¬≠M¬≠an¬≠ag¬≠er" class, the Load¬≠Scene method, to which we pass as para¬≠me¬≠ter a String with the name of the scene we want to load.

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

With that sim­ple instruc­tion we can make the scene change, but for this to work all the scenes have to be added to the com­pi­la­tion, this we do in File > Build Set­tings, open­ing each scene and click­ing Add Open Scenes.

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

When the char­ac­ter falls into the pit a new scene is loaded in which the cam­era auto­mat­i­cal­ly advances and the con­trols are dis­abled, when the cam­era reach­es the end of the tun­nel the GameDe­vLab scene is auto­mat­i­cal­ly loaded again.

Fig. 12: Enter¬≠ing game mode and falling into the pit loads the GameDe¬≠vDi¬≠men¬≠sion scene.


Fig. 13: Tun¬≠nel view of the GameDe¬≠vDi¬≠men¬≠sion scene.

Fig. 14: Tun¬≠nel view of the GameDe¬≠vDi¬≠men¬≠sion scene. 

Fig. 15: Tun¬≠nel view of the GameDe¬≠vDi¬≠men¬≠sion scene. 

Conclusion

To change a scene in Uni¬≠ty we need to import the Uni¬≠tyEngine Scene¬≠M¬≠an¬≠age¬≠ment library and use a sta¬≠t¬≠ic method of the Scene¬≠M¬≠an¬≠ag¬≠er class.

We can refer to a scene by its name or by its iden¬≠ti¬≠fi¬≠ca¬≠tion code that appears in Build Set¬≠tings, in this case we use the name. 

Load¬≠ing a new scene is a rel¬≠a¬≠tive¬≠ly sim¬≠ple task, the prob¬≠lem is that the new scene is pre¬≠de¬≠fined and always starts from scratch. 

For exam¬≠ple a scene can be a house with ten coins, the char¬≠ac¬≠ter enters, takes the coins and leaves the house, ie returns to the pre¬≠vi¬≠ous scene. If we do noth¬≠ing about it, when we re-enter the house scene the ten coins will be inside again. 

To pre¬≠vent this we have to store infor¬≠ma¬≠tion of the state of the vari¬≠ables in the scene and read this infor¬≠ma¬≠tion at the begin¬≠ning of the scene.

YouTube
Scroll to Top
Secured By miniOrange