Sound Effects in Unity

Stephen York
3 min readMar 27, 2021

No game is complete without sound. But I’m not just talking about the music that is in games, I’m talking about sound effects as well. Explosions, gun shots, foot steps don’t just exist; they have to be created and told when to play.

And thankfully, it is very easy to get sound effects in Unity.

To start, create and empty game object and name it “AudioManager”. Then create another empty game object as a child of AudioManager and name it “SFX” or something similar.

You see in the above picture that there is also a background object along with the SFX one. Each of these objects controls a certain aspect of sound. The background object will control the background music that plays during the game and the SFX controls all the sound effects that we will want to play.

Next, add an Audio Source component to the SFX game object.

This component gives you control of the sound. You can specify what sound clip to play, whether or not you want it to start playing when the application starts (“Play on Awake”), whether or not you want the sound clip to repeat, and all the volume aspects of it. But we are just going to handle the AudioClip and we’re assign that through code.

So let’s create an AudioManager script and attach it to the AudioManager game object. Then let’s create two variables, both serialized so we can assign them in the inspector:

The audio clips are an array and will hold all the sound effects that we will play in our game and the audio source is the audio source component that we added to the SFX game object.

Now to play the sound effect, we create a custom method which takes in an integer as a parameter.

This method accesses the audio source and plays an audio clip, that is what PlayOneShot does, and it will play the clip that we pass in. We pass in the specific clip we want to play by accessing the array of audio clips with the integer that was passed into the method. If id equaled 1, we would play the second element in the array of audio clips. Each object that we want to have a sound effect will hold an integer value that corresponds with its sound effect clip in the array of sound effects and will pass that integer to the AudioManager when we want it to play its sound effect.

And the last step is to make sure that someone is listening to the audio. That is accomplished by adding an audio listener to the object that we want to pick up the sound (like the main camera).

And that is how to add sound to your game. All you now have to do is get the sound effects that you want and populate that array.

--

--

Stephen York

An Atlanta based Unity developer, who enjoys running, reading, and creating videogames!