How to Play Sound Effects in Unity

Matej Marek
4 min readApr 28, 2021

--

In the previous article, we have talked about why sound effects and music are essential in every video game. In this one, we will take a look at how we can add them in Unity!

We will be covering four different audio files - and four slightly different variations of how we are going to implement them:

  • Background music.
  • Laser shot, when the player shoots.
  • Explosion sound when GameObjects get destroyed.
  • PowerUp sound on pickup.

Background music

First up, we will be creating a new manager GameObject - an ‘AudioManager’. On this manager, we will create a new empty GameObject, called ‘Background,’ and here we will add a new component, ‘Audio Source.’

Once created, we will simply drag & drop the ‘music_background’ AudioClip to the ‘Audio Source’ component and click on the ‘Loop’ option so that the music will keep on playing indefinitely in the background.

Laser shot

Here we will need to play a sound every time the laser prefab gets instantiated. For this reason, we will be working with the ‘Player’ GameObject and script.

We will add an ‘Audio Source’ component to the Player the same way as we did before, but this time the ‘Loop’ option is turned off, and we will not attach an AudioClip - instead, we will be doing this in the script.

So, let’s open up the ‘Player’ script.

-

Here, we need to create two new variables:

The _laserSoundClip will get populated in the ‘Inspector’ with the ‘laser_shot’ sound effect. The second variable is then assigned in the script, and its ‘AudioClip’ is also set in the ‘Start()’ method.

We need to play the sound when we Instantiate the laser prefab - and we simply call the ‘Play()’ method at the end of our ‘Fire()’ method.

Explosion sound

We handle explosion two ways - one is calling the ‘Explosion’ prefab when the Asteroid or player gets destroyed. The other is via an animation state when the ‘Enemy’ gets destroyed.

The first one is relatively straightforward, as we did previously - now anytime the ‘Explosion’ prefab gets instantiated, the explosion sound is played simultaneously:

When it comes to enemies - here we add the ‘Audio Source’ component in the same way, but we also need to make a change in the ‘Enemy’ script.

Now, before we call the method to destroy the enemy, the explosion sound is played.

PowerUp sound

In this case, we have to go for a slightly different approach.

Since we are destroying the GameObject right as we pick it up, if we were to use the ‘Play()’ method, we wouldn’t hear a sound.

That is because the Play() method plays as long as the GameObject exists.

Therefore we have to use the ‘PlayClipAtPoint()’ method.

We first need to create a variable in which we will store the ‘power_up_sound’ clip.

And then populate this clip in the ‘Inspector’ for all three Power-ups.

Now, we just call the method before we destroy the power-up GameObject:

And that is it! Now our game is even more alive thanks to all the lovely sound effects and the music!

But that is it for now, thank you for reading and feel free to follow me for more articles - and as always, good luck and see you next time!

--

--

Matej Marek
Matej Marek

Written by Matej Marek

Aspiring and eager beginner Unity developer

No responses yet