Managing Scenes

This is an excerpt chapter from my Weekend Code Project: Unity’s New 2D Workflow book. I hope you enjoy this free content on how to use Unity’s new 2D features. If you like what you see, please pick up a copy of the book to get access to the artwork, source code and a PDF/ePub version of the book.

Working With Scenes

Scenes are a critical part of your game. Not only do they represent the levels of your game but they can also be used for splash screens, game over screens as well as places a convenient workspace while you prepare GameObjects and Animations for your game. In this section we will focus on creating some new scenes for our game and switching between them.

Creating A Splash Screen

In this section we will cover one of the most important parts of your game, managing scenes. Unity games are broken up into individual scenes. This entire time we have been working in our main Game scene but now it’s time to create a splash screen and a game over one as well then connect them up to the game.

To get started make sure you save the current scene we are working in the go to he File menu and select New Scene. We are going to call is Splash and save it to our Scenes folder. Now drag the following sprite textures into the scene:

  • splash-background
  • splash-monster
  • splash-start-text
  • splash-title
  • splash-title-mask

From here we are going to want to change the coordinates the sprites in the scene by modifying their X, Y and Order in Layer values to the following:

File Name

X

Y

Order in Layer

splash-background

0

0

-4

splash-monster

1

0

-3

splash-start-text

-2

2

1

splash-title

0.45

-1.6

0

splash-title-mask

0.4

-3.8

-2

The last thing we want to do is modify the Camera’s Size property to 2.5 and make sure we set the Background to Black. Once you do this you will see the following in the Scene Editor.

 

As you can see, Unity’s editor is great for laying out 2D graphics to build basic screens and menus for your game. This a very simple splash screen of course but I wanted to show you how I build these up by layering sprites on top of each other. When you build splash screens like this, it’s easier to animate each of these layers as the player moves from scene to scene. If you look at the splash-title-mask, it’s actually designed to cover the entire background so you can use it to hide everything then animate it down to reveal the splash screen artwork. 

We are not going to complicate this with animation so we’ll just need to add a little bit of flair to direct the player to what they should do in order to start the game. Let’s create a new script called Blink. This script is simply going to modify the alpha of a GameObject to give the appearance that it is blinking to catch the player’s attention. To get started we’ll add the following property to the script:

public float delay=.5f;

Now we will need to modify the Start method:

void Start () {
  StartCoroutine(OnBlink());
}

Finally we will add our OnBlink method below:

IEnumerator OnBlink()
{
  yield return new WaitForSeconds(delay);
 
  float alpha=transform.renderer.material.color.a;
  var newAlpha=0f;
  if (alpha !=1) {
    newAlpha=1f;
  }
  transform.renderer.material.color=new Color(1, 1, 1, newAlpha);
  StartCoroutine(OnBlink());
}

At this point this should be very familiar to you. It’s the same concept we used in the Fade script and we have been taking advantage of StartCoroutine and WaitForSeconds throughout the book. Now all we need to do is attach this script to our splash-start-text GameObject and test it out. 

While it’s nothing fancy, it is enough to give the splash screen a little bit of life. Now let’s talk about how we’ll move from one scene to another.

Moving Between Scenes

Moving between scenes in Unity is relatively easy. We can take advantage of a method on the Application class called LoadLevel. We can simply pass in the name of the scene itself to the LoadLevel method. The only thing to keep in mind is that your scene has to be added to the publishing settings of the game or it won’t work. To get started, let’s run through adding both of our scenes to the publishing tab. Start with the current Splash scene. Go to the File menu and select the Build Settings. From here you will see the following:

This is the area where you can manage what scenes are part of your game’s build. At the bottom of the area on the right hand side is a button called Add Current. Click that and the current scene you are working in, which should be Splash, will be added to the list.

Now close this panel and open up our Game scene then add it to the list as well. You should see that the Splash Scene is the main one with an ID of 0 and that Game is now the next one with an ID of 1.

Now that we have both of our scenes added to the project build settings we can work on switching between them. Go back into the Splash scene and let’s create a new script called ClickToContinue. In it we are going to add the following properties:

public string scene;
private bool loadLock=false;

These two properties will allow us to set the name of the scene we’ll navigate to and set a lock flag so that we don’t keep calling the switch screen logic over and over again if the player keeps clicking. This is very important because if you have some kind of animation that needs to happen before the transition you don’t want to that to keep being called by accident. Now let’s modify the Update method to look like this:

void Update () {
  if(Input.GetMouseButtonDown(0) && !loadLock)
  {
    LoadScene();
  }
}

Earlier on in the book we used the Input class to get the mouse button being pressed. Here you can see we are doing the same thing again but we also test that the loadLock flag hasn’t been set. Now we need to add our LoadScene method:

void LoadScene(){
  if (scene==null)
    return;
  loadLock=true; 
  Application.LoadLevel(scene);
}

Here we make sure that a scene has been defined, and then we set the loadLock to true so we can safely call the Application.LoadLevel method. Now in order to actually use this we will need to attach it to a GameObject. Click on our splash-start-text GameObject in the Hierarchy tab and add the ClickToContinue script to it. We’ll also need to add the name of the scene to go to, so in the inspector panel, type in Game into the Scene value.

At this point you should be able to run the game and test that you can go from the Splash scene to the Game scene by clicking anywhere on in the screen. If it doesn’t work, chances are good that you don’t have the scene added to the build settings. This call will fail silently so make sure you have the scenes correctly added to the Build Settings window and you defined it on the component.

Creating A Game Over Screen

So now that we have a Splash scene that can transition into the Game scene we are ready to add in a game over screen and allow the player to restart the game. To do this we are going to need to make a new scene called Over. Once you have it created, drag the game-over sprite onto the scene and position its X and Y values to 0,0. We’ll also want to modify the camera’s Size to be 2.5 and the Background to Black:

The last thing we are gong to want to do here is drag over the splash-start-text sprite, and attach the Blink and ClickToContinue scripts to it. Position the splash-start at X value of 0 and Y value of -2.3 then set the Scene property to Game for the ClickToContinue component. Here is what the scene should look like when you run it:

You’ll notice that we don’t set this to go back to the Splash scene. We want our player to jump right back into the action so no need to make them click twice to restart the game. The Splash scene is really only for the first time you enter the game, after that the loop should be like this:

We are all done here, but before we can see the Over scene we are going to need to add a way of getting to it when the player dies. Luckily we have an easy way to do this without writing any new code in our game. Open up the Game scene and I’ll show you how we can reuse some of the same code we already wrote to add a simple death animation for the player and link it to the Over scene.

Once you are in the Game scene go to our PlayerCorpse prefab and attach the ClickToContinue script to it. Make sure that you also set component’s Scene value to Over. Now when the player dies it will spawn a corpse and if the player clicks anywhere, it will go to the Over scene. Take a second to test out that it works.

Once the player dies and spawns the corpse you will be taken to the Over screen and from there it’s just another click for the player to start to a new game. See how easy that was? Sure, it’s not perfect and in an ideal world we would want to show the player some text but you could easily create a new script that simply renders the splash-start-text or some kind of click to continue text to render on the GUI layer. Or even better, you can add a timer to make the player automatically transition into the game over screen after a short delay. 

At this point we have created a fully working game. And while it’s not going to win any awards in its current form you have learned enough to move onto the final stage of Unity development which is publishing.

Subscribe To My Mailing List

Want to learn how to make a game? Not sure where to start? Even if you are a seasoned game maker there is still a lot you can learn from my mailing list. I'll be covering tips and tricks for how to build, release and market games each month.

Simply sign up for my mailing list and also get access to a 50% off discount code for my eBooks and other content. I promise to not spam your inbox!

Join Now