Testing the game

The environment created thus far for the coin collection game has been assembled using only the mesh assets included with the Standard Assets package. My environment, as shown in Figure 1.28, features two main floor islands with houses, and a stepping-stone bridge that connects the islands. Your version may be slightly different, and that's fine.

Now, the level, as it stands, contains nothing playable. It's currently a static, non-interactive, 3D environment made using the editor tools. In this chapter, we'll correct this by allowing the player to wander around and explore the world in first-person, controlled using the standard WASD keys on the keyboard. Once the character has been added to the scene, we can then perform our first playtest of the game using the Game panel. Lastly, we will deal with any warning messages shown when we play the game.

Adding a playable character

For the playable character, we will use a ready-made asset, which contains everything necessary to create a quick and functional first-person character:

  1. Open the Standard Assets/Characters/FirstPersonCharacter/Prefabs folder.
  2. Then, drag and drop the FPSController asset from the Project panel in the scene. You will then have a scene that looks similar to the one in Figure 1.34:

    Figure 1.34 – Adding an FPSController to the scene

  3. After adding the first-person controller, click on the Play button in the Unity toolbar to playtest the game:

Figure 1.35 – Unity scenes can be playtested by clicking on the play button from the toolbar

On clicking Play, Unity typically switches from the Scene tab to the Game tab, which will be explored in the next section.

Using the game panel

As we've seen, the scene tab is a director's-eye view of the active scene; it's where a scene is edited, crafted, and designed. In contrast, the Game tab is where the active scene is played and tested from the perspective of the gamer. From this view, the scene is displayed through the main game camera. While play mode is active, you can playtest your game using the default game controls, provided that the game tab is in focus.

The first-person controller uses the WASD keys on the keyboard and mouse movements control head orientation:

Figure 1.36 – Playtesting levels in the Game tab

Important note

You can switch back to the scene tab while in play mode. You can even edit the scene and change, move, and delete objects there too! However, any scene changes made during play will automatically revert to their original settings when you exit play mode. This behavior is intentional. It lets you edit properties during gameplay to observe their effects and debug any issues without permanently changing the scene. This rule, however, doesn't apply to changes made to assets in the Project panel, including prefabs. Changes to these objects are not reverted when you exit play mode.

Congratulations! You should now be able to walk around your level. When finished, you can easily stop playing by clicking on the play button again or by pressing Ctrl + P on the keyboard. Doing this will return you to the scene tab.

Tip

Unity also features a Toggle-Pause button to suspend and resume gameplay.

You may notice that, on playing the level, you receive an information message in the Console window. In the next section, we'll go through the types of messages you may receive, what is causing this specific message, and how we can resolve it.

Understanding console messages

The console window is where any errors, warnings, or information messages are displayed. Sometimes, a message appears just once, or sometimes it appears multiple times, as in Figure 1.37:

Figure 1.37 -The console outputs information, warnings, and errors

By default, this window appears at the bottom of the Unity Editor, docked beside the Project panel. This window is also accessible manually from the application menu, Window | General | Console.

Information messages are typically Unity's way of making best practice recommendations or suggestions based on how your project is currently working. Warnings are slightly more serious and represent problems either in your code or scene, which (if not corrected) could result in unexpected behavior and suboptimal performance. Finally, errors describe areas in your scene or code that require careful and immediate attention. Sometimes, errors will prevent your game from working altogether, and sometimes errors happen at runtime and can result in game crashes or freezes. Errors are printed in red, warnings in yellow, and information messages appear as a default gray.

The console window, therefore, is useful because it helps us debug and address issues with our games. Figure 1.37 has identified an issue concerning duplicated audio listeners. An audio listener is a component attached to an object, which represents an ear point. It enables the ability to hear sound within the scene from the position of the audio listener. Every camera, by default, has an audio listener component attached. Unity doesn't support multiple active audio listeners in the same scene, which means that you can only hear audio from one place at any one time. We are receiving this message because our scene now contains two cameras, one that was added automatically when the scene was created, and another one that is included in the first-person controller. To confirm this, select the first-person controller object in the hierarchy panel and click on the triangle icon beside its name to reveal more objects underneath, which are part of the first-person controller:

Figure 1.38 – Finding the camera on a first-person controller

Select the FirstPersonCharacter object, which is underneath the FPSController object (as shown in Figure 1.38). The FirstPersonCharacter object is a child of the FPSController, which is the parent. This is because FPSController contains or encloses the FirstPersonCharacter object in the Hierarchy panel. Child objects inherit the transformations of their parents. This means that as parent objects move and rotate, all transformations will cascade downward to all children. From the Inspector panel, you can see that the object has an Audio Listener component:

Figure 1.39 – The FirstPersonController object contains an Audio Listener component

We could remove the audio listener component from FPSController, but this would prevent the player hearing sound from a first-person perspective. So, instead, we'll delete the original camera created by default in the scene. To do this, select the original camera object in the hierarchy and press Delete on the keyboard. This removes the audio listener warning in the console during gameplay. Now, if you play the game, the message should no longer appear.

We've come a long way, we've added a first-person character to the game, playtested using the Game panel, and made changes in response to a message shown in the Console panel. Great work! This iteration of playtesting and reacting to messages in the Console window will become second nature as we progress through the book, and understanding and debugging these messages is a big part of developing in Unity.

Now that we have covered the basics, we can work on improving our scene by adding water around our islands and starting on the collectible coins (an important part of a collectible coin game!).