CHAPTER 2
Unity games are organized using scenes. A scene represents a distinct visual portion of the game—a menu, options screen, the screen with the actual gameplay, etc.
Each scene will contain GameObjects and code. GameObjects can be menu buttons, text, input fields, sprites, audio sources, camera, particle systems, and more. Code files will usually be attached to objects or referenced from other code files. This code will control the GameObjects, handle events such as button clicks, play audio files, run AI entities, and engage every other aspect of your game. GameObjects themselves can contain a lot of built-in functionality, but you’ll need to write at least some code in order to make a complete game. Our simple game will contain a minimal amount of code—seven code files and just under 400 lines of code, including “using” sections and white space, which means the actual number of lines is even smaller.
The Scene object in the Unity API is actually a struct with very few members and methods. Unless your game is very complex, you’ll probably never use any of them in your code. Most simple games will move from scene to scene as needed using the methods in the SceneManager class.
The main thing to remember is how your scenes are organized in your project. You’ll need to make sure that all of your scenes are added to the project’s Build Settings, which is shown in Figure 8.

Figure 8: Build Settings Dialog
The first scene in the list will be the one that’s loaded when the game first starts. If the scenes aren’t in the order you want, you can simply drag and drop them until they are.
Typically, scenes will be added to the Build Settings automatically as you add them to your project. If for some reason you have scenes that don’t show, simply open them and use the Add Open Scenes button in the dialog.
The SceneManager class is responsible for handling the transitioning from scene to scene. Table 1 shows two methods for doing this.
Table 1: SceneManager Methods
Loads the scene by its name or index in Build Settings. | |
Loads the scene asynchronously in the background. |
The overloaded signatures of the two methods are shown in Code Listing 1.
Code Listing 1: LoadScene Signatures
public static void LoadScene(int sceneBuildIndex, SceneManagement.LoadSceneMode mode = LoadSceneMode.Single);
public static void LoadScene(string sceneName, SceneManagement.LoadSceneMode mode = LoadSceneMode.Single); public static AsyncOperation LoadSceneAsync(int sceneBuildIndex, SceneManagement.LoadSceneMode mode = LoadSceneMode.Single); public static AsyncOperation LoadSceneAsync(string sceneName, SceneManagement.LoadSceneMode mode = LoadSceneMode.Single); |
public static function LoadSceneAsync(sceneBuildIndex: int, mode: SceneManagement.LoadSceneMode = LoadSceneMode.Single): AsyncOperation;
Both methods take either a string (the name of the scene) or an integer (the index in the build settings) as the first parameter. The second parameter is an enum specifying the mode for the scene loading, as in Table 2.
Table 2: LoadSceneMode Values
Closes all currently loaded scenes and loads a scene. | |
Adds the scene to the current loaded scenes. |
Most of the time, using LoadScene will be sufficient, as scenes will load with no more than a small delay. When you make larger games with larger levels, you might look into LoadSceneAsync.
The Scene struct has a number of members and methods, but for simple games it would be extremely unusual to need them. If you start writing scripts to use in the Unity editor, they might come in handy, however.
We’ll need one more scene that we’ll use for our actual gameplay. You can either right-click in the Scenes folder and select Create | Scene or use the Assets menu. I’ve simply called the scene Game. If you haven’t already saved the scene that was added when the project was created, you’ll be prompted to save it. Make sure you save it in the Scenes folder in order to keep things organized.
When you add a scene to your project, it comes with a Camera object. The camera controls what gets shown to the player. For a 2-D game, there’s not much you’ll need to do for the camera. As long as your sprites stay within the white rectangle that surround the camera icon in the scene window, they’ll be displayed when the game runs. As Figure 9 shows, the Camera Preview that shows in the scene window when you select the Camera object in the Hierarchy window will let you know what the scene looks like when the game is run—without requiring that you switch over to the game window.

Figure 9: Camera Object Selected and Camera Preview
One thing you’ll probably want to do with the camera is change the Background property from the default blue color. Click on the colored bar next to the Background label in the Inspector window to display Color picker dialog shown in Figure 10.

Figure 10: Color Picker Dialog
You can click on the circle in the color box and move it around to change the color or enter the RGB or hex values in the text boxes. In order to set the background to black, you can drag the circle to the bottom left or enter a 0 in each of the RGB text boxes. This will give you black, which I almost always use for the backgrounds of my scenes.
There is very little code in the game that is scene related, although we will end up with code in the Menu scene in order to load the Game scene and vice versa. If you want to take a look at code ahead of going over it here, look in the Script folder in the sample project. If you load the project in Unity, you can double-click a file to open it in Monogame or Visual Studio (if you have the VS for Unity tools installed). There’s a High Score scene in the sample project, but there’s no code to support it. If you want to implement functionality in order to keep track of scores, you add code to display it from the Menu scene by adding another button and using the same code logic we’ll address shortly.
Obviously, a more complicated game would have more scenes and more scene-related code. A game with multiple levels would have code to load the different level scenes (if the levels were not data-based or able to be handled by one scene).
The code in the Menu scene loads the Game scene when the player selects the difficulty, as shown in Code Listing 2.
Code Listing 2: Difficulty Button Code
public void DifficultyButton_Click(int index) { Globals.DifficultyLevel = index; SceneManager.LoadScene("Game"); } |
Code Listing 3 shows that the code in the Game scene is virtually the same.
Code Listing 3: Exit Button Code
public void ExitButton_Click() { SceneManager.LoadScene("Menu"); } |
Most of your scene code will look this way. The coding is very easy, despite it being such an important part of your game. You’ll spend 99% of your development time with scenes working in the Unity editor, laying out objects and tying code into them, not writing code specific to managing the scenes.
You’ll notice the methods seem to be handling the click event of buttons, but there are no buttons in the scenes yet. We’ll add them and other interface elements in the UI chapter, coming next, and see how to hook up the code to the interface.