Adding sound to a game in Unity

3D Game Design with Unity

🕑 This lesson will take about 20 minutes

In this lesson, you will learn how to add sound to a scene in your game. You can add sounds that play constantly in a scene such as background music and you can also add sounds that only occur in specific locations in the game, at specific times, or after different events such as a collision. Sounds can also be looped or just play once.

There is also 2D sound (heard everywhere in your scene) and 3D sound (heard only when you are within a specific proximity to an object and that gradually gets louder as you approach the object and quite as you move away from the object). Sound can be controlled with code but you can also add sound without any code at all.

Watch the video below or scroll down to read the step-by-step instructions and sample code.

Is YouTube blocked at school? Watch on Google Drive.

To add audio to your scene, follow these steps:

  1. Add an object (eg. empty object, cube, sphere) or use an existing object in your scene to add the sound to.

  2. Select the object and click Add component in the Inspector panel.

  3. Add the Audio source component to the object.

  4. Import an audio file into your Assets folder (eg. MP3, OGG file) and then add it to the ‘audio clip’ property on the audio source component in the inspector panel by dragging the file there.

  5. Note: Make sure there is one (and only one) Audio Listener component in your scene. It should be attached to the character controller’s camera or main camera. Delete or disable any additional audio listeners that might be attached to objects in your scene if needed to ensure there is only one in any scene.

  6. To make the sound play throughout the whole scene, enable ‘Play on awake’. You can also check the ‘Loop’ option to make the sound repeat/loop. Play the scene to test your audio.

  7. To use the audio file as 3D audio, drag the ‘Spatial Blend’ slider from 2D to 3D – this means that you will only hear the sound when you are near the object. You can change some of the properties to specify how it fades away and how close to the object the player needs to be to hear the sound.

To play the audio, use this code:

GetComponent<AudioSource> ().Play();

To stop the audio, use this code:

GetComponent<AudioSource> ().Stop ();

You could also add an ‘if’ statement eg. to make the sound play when a key is pressed. For example, you could add this ‘if’ statement to the Update() method.:

if (Input.GetKey(KeyCode.Space)) { GetComponent<AudioSource>().Play(); }

Also, you can check if the audio is currently playing by using .isPlaying(). Any looping sound or a song can be turned on and off using an if statement that checks if the audio is currently playing – just add something like this to your Update() method.

if (Input.GetKey (KeyCode.Space)) {
if(GetComponent<AudioSource>().isPlaying == true){
GetComponent<AudioSource>().Stop ();
}
else{
GetComponent<AudioSource>().Play ();
}
}

How to change the audio clip using code?

If you want to change the audio clip on the audio source during the game (maybe after a certain event or when a key is pressed), you will need to add the following line inside the class (outside of all other methods) in your script:

public AudioClip otherClip;

You will then need to stop the current audio clip from playing before you switch to another audio clip and start playing that one. Inside the Update() method (or whichever method you are controlling the audio from), you will need to add the following code. This code may be placed inside an if statement (eg. if x key is pressed):

GetComponent<AudioSource>().Stop ();

Then, after you have added that line of code you can switch the audio clip and start playing the new audio clip using this code:

GetComponent<AudioSource>().clip = otherClip; GetComponent<AudioSource>().Play ();

Lastly, you will need to go into Unity and drag the audio clip file (eg. MP3, OGG file) onto the ‘otherClip’ property on the script component (for this script), that is attached to the object containing the audio source, in the Inspector panel.

Next lesson: Animating 3D objects