Adding sound to your scene

In this tutorial you will learn how 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 every now and then or that occur after an event. 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 in a specified proximity to an object/sound). Sound can be scripted 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.

To add audio to your scene, follow these steps:

  1. 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 other audio listeners as there can only be one in any scene.
  2. Add an object (eg. empty object, cube, sphere) or use an existing object in your scene to add the sound to.
  3. Select the object and click ‘Add component’ in the Inspector panel.
  4. Add the ‘Audio source’ component to the object.
  5. Find an audio file (eg. MP3, OGG file) and add it to the ‘audio clip’ property on the audio source component in the inspector panel.
  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 script the audio, add this code to the Start() method:

GetComponent<AudioSource> ().Play();

and to stop the audio:

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 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) in the Inspector panel.