Learning Unity — Switching Unity Scenes In-Game
In the last article, we continued exploring the Unity UI system. In this article, we’ll be learning how to switch between Unity scenes.
Unity scenes are the environments in which we construct our games. Everything we see within a Unity game is contained in a scene, or possibly multiple scenes. By default, when a new Unity project is created it generates a scene with a camera and light.
Why would we want multiple scenes? We can use individual scenes to represent different levels in the game, to include “mini-games” into our game, or to separate various menus from the core gameplay.
Up until now in the Learning Unity series of articles, we have only ever worked in a single scene, but this time we will be learning how to move between scenes in a game.
To create a new scene in the project, right-click in the Project window and select the Create → Scene option. We can see the new scene by loading it in the Unity Editor. This can be done by double-clicking on the desired scene in the Project window, or by clicking the Open button in the Inspector window while the desired scene is selected.
Now we have multiple scenes, but before we can switch between them we need to add them to our project’s build settings. We can visit our project’s build settings by going to File → Build Settings.
There are many options that we can configure in the build settings screen, most notably the target platform of the project (we’ll be going over that in greater detail in a future article). For now though, what we are most interested in is the “Scenes In Build” section. Any scenes that we will want to load in the game will need to be added to this section.
We can add our scenes to the build quite easily by opening the scenes in the Unity Editor and clicking the “Add Open Scenes” button. Once we have all relevant scenes added, we can continue to the next step.
To be able to switch scenes through scripting, we need a script. A script will need to be attached to a game object, so we’ll need to create one for our scene first. It can be any type of object, even an empty object. Once we have created the object, we will then need to create a new C# script and attach it to our object.
Within the script, we will first need to add the SceneManagement library. In the using statements section, add a new line for this library.
using UnityEngine.SceneManagement;
The other scene will be loaded using the LoadScene method of the SceneManager class. Naturally, we can have whatever conditions we want to cause the loading of a new scene. It could be interacting with a trigger, a certain amount of time passing, whatever we want. In this example, the second scene will be loaded by pressing the space key.
In this example, the LoadScene method is taking the name of the desired scene as the parameter. However, there are other ways to determine what scene will be loaded. Feel free to review Unity’s documentation to see the alternative methods.
This is all that is needed to open a new scene in-game, but before we start up this game to check it out, we should take a moment to make our two scenes visually distinct. That way we can see for ourselves that the script is working.
And here we can see that the script takes us from one scene with two objects in it to another with just one. That concludes this introduction to scene switching. In the next article, we are returning to animations and learning some more advanced practices. Until then, good luck and happy coding!