Scenes are part of every game. Changing scene based on the game’s story or based on the menu is a common functionality required in all game engines. In Unity, the Scene Manager does that job. The Unity Scene Manager, as its name suggests, manages your scenes at runtime. It is a well-equipped class of Unity engine that we can use to effortlessly switch between scenes. We will cover all of its major functionality in this article, and we will also run you through step-by-step guides on how to apply them.
First, let’s start with the basics of the Unity Scene manager’s script. All of the scene manager’s properties and methods are static. This means you can access them from any script without having to reference them. I will demonstrate this in the coming example.
To be able to use the Unity scene manager, you need to make sure you have included UnityEngine.SceneManagement in your script header as shown in the image below.
Unity Scene Manager Properties
The scene manager has 2 basic properties:
- sceneCount: Total number of scenes that are currently loaded
- sceneCountInBuildSettings: How many scenes are currently added and active in your Unity Build Settings
These properties can come in handy if you need to keep track of the number of loaded scenes. Here’s an example script that uses the Unity scene manager properties.
You might be wondering, how can there be more than one scene. Well, this is possible, and it’s called Additive loading. You can load a scene on top of another scene, and have them both renderer and run their scripts at the same time. This may be bad for performance, so please keep that in mind when using it.
Unity Scene Manager Functions
You can check which scene is currently loaded by using SceneManager.GetActiveScene(); This will return a string with the scene name that is currently active. In the case where there are multiple scenes active, it will return the first scene that was active.
Unity Load Scene()
Loading a scene is very simple. You simply need to call the method SceneManager.LoadScene(); and tell it which scene you would like to load. Unity load scene function takes a string as input but it can also be overloaded with a number.
On line 18, I check if I have more than 100 points, if I do, then I load the scene called “Level 2”. It’s that easy. You just need to pass the name of the scene to the Unity Load Scene function. A common mistake that people do is using capital letters. This function is case sensitive so be careful while typing the scene name.
As mentioned before, you can also load a scene on top of another scene. For this example, let’s say I just completed the game, and want to load a leaderboard. I could do this on the same scene, but if for some reason I need to have the leaderboard on a separate scene, I can simply load it onto my current scene and use it as I normally would. You can also create a leaderboard within the scene similar to creating a pause menu.
In this example, when I get 100 points I am loading the leaderboard scene, then I am looking for and storing it using GetSceneByName and then storing it into a Scene variable, and then I am setting that as the currently loaded scene. Now whenever I want to check which scene I am currently on, I will get “Leaderboards” as a result.
It is very similar to loading a scene normally, except you also add a parameter to specify whether the scene should load additively, or as a single scene. A single scene of course means that there will only be a single scene loaded.
Note: If the LoadSceneMode parameter is not added, the scene will load as Single by default.
Another important feature is that when you instantiate an object in Unity, it will put that object into the currently active scene, so if you have multiple scenes loaded, and you would like to instantiate an object, or delete an object by name, or reference any object, Unity will perform that action based on your current active scene. This method lets you choose which scene is the currently active scene.
Creating a Menu in Unity using Scene manager
Unity scene manager can be used to do a lot of things. The most common example is the main menu. You can switch to a different scene when you press a button in the menu. Let’s see how to create a simple menu using loadscene() function.
The very first step is to create a script. I will be using the same script as the other examples in this tutorial.
Important note: The method must be public, otherwise it cannot be accessed outside the script. This includes buttons as well, so bear that in mind.
Now that we have the script setup. Let’s set up the scene and make use of the method we created.
1.Right-click in your hierarchy, and navigate to UI > Button to create a button in your scene (This will automatically create a canvas if you don’t have one). You may use Button, or Button – TextMeshPro. For this example, it does not make any difference. You can name the button whatever you like and can change the text properties to whatever you like as well.
2. Right-click in your hierarchy and select Create Empty. This is where we will put our script that holds the level-changing method
3. Rename the Empty to whatever you like. I will name mine SceneManager.
4. Attach the script we created in this tutorial to your new SceneManager Empty GameObject you just created. You can do this by selecting Add Component in the inspector and searching for the script by name. In this tutorial my script is called “ExampleScript”, so that’s what I will search for and attach.
5. Select the button game object you created earlier
6. When your button is selected, in the inspector window, find the button component and find the On Click Event section.
7. Click on the + button on the bottom right of the On Click event section to add an event
8. Drag the SceneManager game object into the new event created
9. Click on the No Function drag down menu
10. Navigate to, and select ExampleScript > LoadScene
11. Type in the name of the scene that you want to load when this button is pressed. Make sure that the spelling of the scene is exactly the same as the one in your project. It is also case sensitive, so an even better option is to copy/paste the name from your scene asset into the field.
12. Finally, run the scene and press the button. Your new scene should load!
That’s it, you have successfully created a menu button.
Note: To exit a game you can use the Application.Quit() method. It doesn’t work in the editor but once the game is built, you can test it on the target platform.
Unloading a scene
If you have multiple scenes loaded, you should always unload the scene that you are not using. This will improve your game’s performance, and would also keep your project clean as you are working.
You can Unload a scene by using SceneManager.UnloadSceneAsync(); Async means that it will unload your scene asynchronously. This is good, as it won’t interrupt the game, so you can unload scenes and continue playing/working at the same time.
Similar to the previous examples, I have just added a line at line 24, which means after loading the leaderboards, I unload Level 1 so that I can now only see the leaderboard.
This is pretty similar to simply load the leaderboard scene as a Single. But by adding these extra steps, you can maybe play some animations, some audio and particles, or whatever it may be in your scene while the leaderboard is up. Then when all your processes on Level 1 are complete, you can unload it.
unity Scene Manager Events
There are 3 events associated with the Unity Scene Manager, and they can come in handy in a lot of situations:
- SceneManager.activeSceneChanged: This event is triggered whenever the active scene is changed. We have demonstrated this in the previous pages here.
- SceneManager.sceneLoaded: This event is triggered whenever a new scene is loaded. A scene that is loaded is different from active. When a new scene is loaded, the activeSceneChanged event is NOT called. That event is only called when you explicitly decide to set the active scene.
- SceneManager.sceneUnloaded: This event is similar to the sceneLoaded, it is triggered when a scene is Unloaded.
You can use these events by subscribing to them just as you would with any other Unity event. And when a scene is loaded, for example, you can do something in your game. Here’s a code example.
We first need to hook up the Events so that when they get invoked, we can call a method. On lines 16 and 17, we subscribe to the scene manager events. This means that whenever a new scene is loaded, the method OnSceneLoaded will be called. And whenever a scene is Unloaded, the method OnSceneUnloaded will be called.
This is useful if you want to do something specific when a scene is loaded and unloaded, for example, when a scene loads, you may want to initialize some variables to prepare the new level. You could also use this to save your game, too. The possibilities are endless.
If you don’t need to do anything, you do not need to use the events, the scene manager methods work regardless.
As you can see the Unity scene manager is of many uses. You can test out the different function in different ways. If you need clarification on anything feel free to leave a comment below.