Galaxy Shooter 2D — Loading Scenes in Unity #12
I’ve recently made a simple Main Menu scene for my Galaxy Shooter game. I have some design ideas that would greatly improve the menu, but this placeholder-y menu will do, for now.
But, a main menu isn’t complete until it can load scenes from your game. So, today, it’s all about loading scenes from my Main Menu.
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”.
Also, the “New Game” button, when pressed, will trigger an event that will load the Game scene. To do so, we would need a script with that functionality. For now, I’ve made a very simple “MainMenu” script and I will load the level there. Not optimal as you would want to have another script solely focuses on Level Management, but that’ll come soon enough.
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.
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.