Galaxy Shooter 2D — Loading Scenes in Unity #12

A simple guide to switching between levels in your game!

Set-up

Before we start writing the code to switch between levels, we must first select our scenes and link them in the Build Settings. Like variables, we are storing a reference to our scenes in a Unity class called “SceneManager”.

Notice that there are some numbers at the right side of the scenes. That’s their associated build index and those integers can also be used to load their respective scenes.

How to start loading scenes

In my “MainMenu” script, I’ve included the UnityEngine.SceneManagement library (you won’t be able to load scenes without it), and wrote a new method named LoadGame(), which will contain the loading functionality.

I’ve used the build index of the Game Scene to load the scene, however, I could’ve used a string as well

Conclusion

With one line of code, we can start switching between scenes! It looks simple enough, but it holds more functionality that will allow you to create cool transitions or loading screens. But that’s a big topic on its own.

I am a recently graduated Game Designer with strong knowledge of the Unity Engine! You can check my game “Noise Hunters” on Steam!