Changing Scenes with Unity’s SceneManager

This is just a quick article on how we can load scenes in Unity (and I’ll try to give a brief overview about some code which might come in handy). To reload a scene, load a different one, or retrieve information about the current scene you’ll first need to include the ‘using UnityEngine.SceneManagement’ declaration at the top of your script. Once we’ve done that, we just need to refer to the SceneManager class into access some of the methods it contains (namely, ‘LoadScene’):

This is something I’m using in my games. Admittedly, it’d probably be better to use a try and catch statement, or more specifically perform a check to see that the specified scene name is valid, but this is fairly easily resolved. In the above code, we can type in the scene we want to load in the inspector and then call the LoadScene() method from, say, a UI button in the scene.

In C# programming, while we can’t typically have two methods with the same name within a single class we can in fact ‘overload’ the method by making sure they have different parameters. By doing this, I could specify which of these methods I want to use based on whether or not I supply a scene name as an argument. I’d generally recommend including a method like the second one, as it prevents the code getting needlessly messy with dedicated methods for loading specific scenes. And as these are both public, I can call LoadScene from another script provided I have a reference to an instance of this one (e.g. LoadScene(“Tutorial”), LoadScene(“Main”), LoadScene()).

A couple other (hopefully) useful things:

To check what the current scene is:

If we need to perform any logic based on what the current scene is

To reload the current scene:

Reloads the current scene whenever the player presses R

Loading next or previous scene in build:

Lastly, do make sure to add the scenes you want to load and include in your build under File > Build Settings. You can drag scenes from your assets into this view, or select Add Open Scenes to add the one you’re working on:

And with that, I think that’s about all I have to share on getting up and running with Unity Scene Management 😉

Game Developer | Game Design and Literature Graduate