Member-only story
Unity Game Development — Sound Effects in Unity

Having set the background music, I now want to begin adding more sound effects to my game. The first effect that I sorely seem to be missing is the sound of the cannon firing. I will add this AudioClip and a couple of others to begin building my game’s soundscape.
As I mentioned in my previous article, Unity uses an AudioListener Component to detect AudioClips. There can only be one AudioListener in any given scene. However, you can use as many AudioSource Components as you need.
Objective:
Add sound effects to my player’s shooting abilities.
Method:
I will begin by adding an AudioSource to my tank’s turret.

This new AudioSource will play AudioClips as needed. Therefore, I don’t have to assign a clip to it yet.
In my Player Script, I will include the clips I need by adding two fields of Type AudioClip.

The AudioClip Type holds audio format files that Unity can interpret to play sounds.
Once I have created these fields, I can populate them in the Inspector by dragging the desired audio files into their respective places.
Next, the Player Script will need a reference to the AudioSource Component. I can create a variable of Type AudioSource. I can assign the value of the variable in Start()
using GetComponent()
.

I can now take advantage of one of the public methods that Unity provides for the AudioSource Component.
