CHAPTER 7
Very few games are complete without some kind of sound effects, background noises, or music. Even the simplest of games like Bejeweled or Tetris have sounds. Many AAA games have such good sounds that if you closed your eyes, you might think the game was the real world.
At its simplest level, sounds in Unity involve two objects—the AudioClip and the AudioSource.
The AudioSource has a ton of properties for dealing with sounds in both 2-D and 3-D games, but we don’t need to use any of them. AudioSource is simply a container for the audio file we need to play.
We’ll have four different audio files that we’ll be playing—one music file that will play in the menus and three sound effects in the game. You can use either the audio files provided for this chapter or use your own files. Unity supports the .aif, .wav, .mp3, and .ogg audio file formats.
Add the files to the Audio folder. In the Menu scene, right-click the Hierarchy and select Audio | Audio Source. Drag the file “warg_-_kibelebassrif.wav” onto the AudioClip property of the AudioSource. Make sure the Play On Awake and Loop check boxes are checked. The Play On Awake property starts the file playing when the scene starts. While the scene is open, the Loop property will restart the file each time it finishes. None of the other check boxes should be checked. The Mute check box would cause the music to mute, and the other check boxes are for functionality that’s not used.
After adding the AudioSource and selecting it, you’ll notice that there is a circle in the scene that encompasses the entire Canvas, as shown in Figure 37.

Figure 37: Audio Source Area
The circle defines the area in which the sound will be heard for a 3-D game. If you open the 3-D Sound Settings section of the AudioSource in the Inspector, you’ll see the settings that define how the sound will be heard in a 3-D game. For our purposes, they don’t matter. The sound will be heard no matter what.
Even the settings that can affect the sound in a 2-D game, which you’ll find above the 3-D Sound Settings section, will rarely need modification. Perhaps you might change the Volume setting if you want to give the player the option of disabling sounds or modifying the volume.
For the main part of the game, we’ll need three AudioSource objects. Change the scene to the Game scene and add them. Unlike with the menu, you’ll want to change the names of the objects. I used BulletAudioSource, GhostAudioSource, and PlayerDeathAudioSource. Drag the appropriate sound files to the AudioSource objects. None of these will be set to loop or play immediately. We’ll control when they will play with script.
Note: Playing sounds from a prefab does not work.
Now that we have the AudioSources added to the scene, we need to modify the Game script in order to recognize the sources. Add the code from Code Listing 16 to the declarations section.
Code Listing 16: Game Audio Declarations
public AudioSource GhostAudioSource; public AudioSource PlayerDeathAudioSource; public AudioSource BulletAudioSource; |
Save the script and go back to the Unity IDE. Select the object to which you’ve added the Game script and you should see the three AudioSource objects that you just added in the Inspector window. Drag each AudioSource object from the Hierarchy window into its corresponding object in the Inspector, as shown in Figure 38.

Figure 38: Audio Source Script Members
Next, add the following line to the Globals script.
Code Listing 17: Global Bullet Audio Source Declaration
public static AudioSource BulletAudioSource; |
Now add the following line after the If statement in the Start method.
Code Listing 18: Bullet Audio Source Assignment
Globals.BulletAudioSource = BulletAudioSource; |
Because the code that spawns the bullets is in a different script file, we’ll need to get to the AudioSource object in the code. Making it a global will allow us to do that. In order to actually play the bullet sound, insert the following code at the beginning of the If statement at the end of the Update method in the PlayerController script.
Code Listing 19: Playing Bullet Audio
Globals.BulletAudioSource.Play(); |
Next, we’ll handle the ghost spawning. In the Game script at the end of the SpawnGhost method, add the code from Code Listing 20.
Code Listing 20: Playing Ghost Audio Logic
//If hard difficulty, only play sound every other time. if (Globals.DifficultyLevel == 2) _playSound = !_playSound; if(_playSound) GhostAudioSource.Play(); |
Playing a sound when the ghost spawns is a nice touch, but you must always think about what can happen when a sound plays. In this case, with the different difficulty levels, the ghost could spawn at different times. In the case of the hard difficulty level (DifficultyLevel == 2), that means every half-second. If you haven’t already tried the game without the code, do so now. You’ll see the problem almost immediately. The sound plays a bit too often, playing over itself. This will get annoying very quickly. The code we add will play the spawn sound every other time a ghost spawns, which makes it a bit more tolerable. You could change the frequency so that the sound plays even less, but that will mean a bit more code and you’d have to change from using a Boolean to track when the sound plays. Not difficult, but I chose to go the easy route. Feel free to modify the code to limit the sound further if you prefer.
The above code uses a switch variable we need to declare. Put the following code at the end of the declarations section of the Game class.
Code Listing 21: Ghost Sound Switch
private bool _playSound; |
We’ll need to set the variable to true in the ResetGame method. We can’t do it in the declaration because the value might end up being false if the game is played on the hard difficulty level. Add the following code to the end of the method.
Code Listing 22: Ghost Sound Switch Initialization
_playSound = true; |
The last sound is played when the character dies as a result of his health reaching 0. Insert the following code at the beginning of the Update method.
Code Listing 23: Playing Code for Player Death Sound
//PlayerDeathAudioSource.Play(); |
We will add logic late in order to figure out when to play this sound, but we’re adding the placeholder now so that we’ll know where the logic goes.
That’s all the code we need to play our sounds. These are very easy sounds that don’t require a lot of logic to handle. You can add sounds that play when they hit a collider. You can trigger sounds with other types of logic. There are a number of other scenarios in which playing sounds can happen. Feel free to experiment. One idea is to play sounds when you click UI buttons, for example. The source code contains a sound file you might use for that purpose.