In this article, I am going to be showing you some different ways to add sound to your game.
The first way we can add sound to our game is playing background music. In this, we will create GameObject (“Audio Manager”) and make a Child GameObject (“Background Music”). This Child GameObject will be the one playing the music, so will need to add an Audio Source component to it. Without, the Audio Source component we wouldn’t be able to play any kind of sound or music. In the Audio Source component, you have several settings you can use to customize the sound, but in this article we will just focus on playing the sound. One aspect of the Audio Source is the AudioClip. This is where we can assign a sound or music clip that we will want to play.
With the AudioClip is loaded, we will want to set Play On Awake and Loop to true. This will play the AudioClip when the GameObject is created and continue to play the clip in a loop. This method is great way to get background music into the game and script wasn’t even needed.
This next method of adding sound will be a way to trigger a sound from a script. The example will be making a laser sound every time we fire a laser, which is happening on the the Player script.
Since we are adding sound to the Player, we need to add an Audio Source component to the Player GameObject. You need an Audio Source to play sounds.
First, we will create a AudioSource and AudioClip type variable and add a Serialized Field attribute.
We will assign the Audio Clip we want to play when we fire in the Inspector on the Player script.
This could have been done by assigning the Audio Clip to the AudioSource component and it still will work fine. This way avoids running into any issue of not assigning items in the Inspector, but will be assigned in the script. We will need to get a handle of the AudioSource and in the On Start will we will get the AudioSource and assign it to our variable.
For best practice we will complete a NULL check to make sure we grabbed the AudioSource component and if we do have it, we can assign the Laser Sound Clip (that we already assigned in the Inspector) to the Audio Source so it will play.
Everything is now setup for the audio clip to play and we just need to call on it when we need the sound to play.
The Laser Audio Clip will need to play when we fire the laser, so in this method we will add it using the AudioSource.Play(). This will play whatever audio clip is assigned to the Audio Source.
This next method of trigger audio is a different variation of the AudioSource.Play() method. We are going to add an explosion sound effect to our enemy for when they are destroyed. The setup is the same with adding an AudioSource component to the Enemy Prefab then creating a AudioSource and AudioClip variable to the Enemy script. Getting the Audio Source component and assigning it to our AudioSource variable. Doing a Null check and assigning the explosion AudioClip to the AudioSource variable.
The difference is how we call the sound or trigger the AudioClip. We are going to use the AudioSource.PlayOneShot() for this sound effect.
Do not forget to pass in the AudioClip you want to play and you can also pass in a volume float (0–1).
This will be added to OnTriggerEnter2D collision when the laser and player hits the enemy. The benefit of using the PlayOneShot is you can play multiple shots at a time and they will not cancel each other out like the AudioSource.Play method. This is perfect for collisions because there can be multiple collisions happening at the same time and we want to hear them all, not just one.
We could have had an issue with the audio being cut short because the AudioSource is attached to a GameObject that is being destroyed. This doesn’t happen because there is a delay added to the destroying the Enemy GameObject, which allows the clip to play out before the Enemy gets destroyed.
The last way to trigger a sound comes in handy when you do need to add audio to a GameObject that is going to be destroyed instantly. Remember, if an AudioSource is attached to the GameObject and it is destroyed the audio will either be cut off or will not even play. In this example, we are going to add a sound effect to the PowerUps when they are collected.
On the PowerUp script, we only have to add a Serialized Field AudioClip variable. Assign the audio clip you want to play to the PowerUp script in the Inspector. We are going to use AudioSource.PlayClipAtPoint().We will need to pass in the clip we want to play, a position, and you can add a volume float (0–1). We didn’t have to add an AudioSource component to the PowersUps because this method will create one, play the clip and destroy itself. Even if the GameObject is destroyed, the audio clip will still play.