Unity Dev Blog: Loading Scenes in Unity

Every game needs a menu screen, right?

While I cannot speak for every video game or application in existence, I can say that this game does need a main menu screen. It will be a simple one, but it will allow me to demonstrate a few more important concepts in Unity.

The main topic today is loading a new scene (or switching scenes) during game play. I will also cover using clickable buttons on the user interface to allow the user to preform certain actions.

The main menu screen for this game is not overly complex. It has the game’s logo (and yes, I realize it says Galaxy Shooter and I’ve been calling it Space Shooter…) as well as some simple text instructions on how to play. Lastly it has a NEW GAME button which will actually start the game.

I used the same background image from the game for the background for this scene, making sure to set the Canvas Scaler options to allow the image and game objects to scale correctly with any screen size.

The code for ‘NEW GAME’ button was put into a MainMenu script just for this menu scene and attached to the Canvas game object.

That’s is the entire script so far.

I added the UnityEngine.SceneManagement name space at the top and the script currently only has one function, LoadGame(), that gets called when the user clicks the ‘New Game’ button. Next, you just reference the script in the button’s OnClick section and have it point to the function you want to call.

Drag in the Canvas for the reference. Then select the function you want to run when the button is clicked.

Pretty straight forward, but there are a few things you need to know about loading or changing scenes in Unity.

  • The scene you want to load MUST be in the Scenes in Build list under the build settings window (see screen shot below).
  • You can access the scene by name but it has to be spelled exactly the same (which I prefer because it is easy to see what is happening in the code) or by Index (which you can get from the Build Settings window.
  • You will need to add using UnityEngine.SceneManagement to the script (see the code above).

You can drag in all of the scenes in your game from the Assets folder in the Project window. You can also rearrange them in this list. The first scene listed will be the first scene Unity will run when the stand alone project is compiled and ran.

Again, in a stand alone, compiled Unity project will start with the first scene in this list — the one at index (0). In the editor, however, the currently loaded loaded scene will run.

I also added the ability for the user to restart the game after they run out of lives.

I created a simple GameManager script to handle this. It has two functions inside that handle telling the rest of the game that the game has ended and then waits for user input.

Notice that this is in the Update() function of the script so it is ALWAYS running and waiting for the ‘R’ key to be pressed but it will not do anything as long as the _isGameOver boolean is set to false.

In the UIManager, the GameOverSequence() function shows the Game Over text and restart instructions, as well as calling the GameOver() function in the GameManager to set the _isGameOver boolean to true, letting the GameManager know it can now act if the ‘R’ key is pressed.

So, loading new scenes into unity is easy to manage. Menu screens, new game levels, shops in RPG games, etc. are just a few good places to need to load in a new scene but if they are really simple, then they can also just be a simple Canvas overlay that is hidden until needed. There are plenty of options on how to tackle this user experience.

Tomorrow, as long promised, I will be adding more visual effects to the game. No more teasing, enemy ship explosions are coming! Here is a small preview:

Tip: if you are using individual images for each frame, you can ‘preview’ the animation by scrolling through the list of image files :-)

See you there!

5

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Michael Hatfield

Michael Hatfield

Math and science teacher / self-taught programmer and Unity game developer working hard to get into the game development industry fulltime.