How to Play Sound Effects in Unity

Chris Hilton
Geek Culture
Published in
4 min readJul 16, 2021

Objective: To add some sounds effects to our players lasers and an exploding asteroid in Unity!

Tenor.com

Now that we have previously ran through the basics of sound by adding some background music to our scene and understanding that we require an Audio Listener to play music in a game, it is time to add some cool sound effects to our players lasers and an exploding asteroid.

Adding a Sound Effect to our Laser

To get started, let’s create an empty game object to our Audio Manager as a child and call it “Laser_Shot_Audio_Clip”. Next, add an Audio Source component to this game object and drag in the audio clip you wish to play to the appropriate box as shown below (or click on the button symbol):

Now also make sure that ‘Play on Awake’ is turned off, as we want to control when the sound is played through code when the player fires their laser, and we also don’t want this to loop!

Adding Some Code to Our Player Script

We want to be able to call and play this audio source when the player shoots the lasers, so let’s add some code to our existing FireLaser() method:

Let’s firstly create a handle to the audio source component and then within the Start() method we need to locate the audio source that is on the game object “Laser_Shot_Audio_Clip”. Of course, make sure to null check that we are indeed successfully using script communication to access this audio source.

Then within our FireLaser() existing method, let’s add one line which is to Play() the audio clip located within the audio source component. Play() is a built in Unity method within the audio source component and it simply tells the component to play the clip.

We also added this line outside of the if/else statement as it didn’t matter which lasers were being shot (single or triple shot), we still wanted the laser audio clip to play regardless of how many it was shooting.

You should be able to hit ‘Play’ and listen to your sound as you shoot your lasers!

Adding a Sound Effect to Our Asteroid

We are going to look at this one a little differently. As we have an existing “Explosion” script and explosion prefab game object, we are going to attach the audio source component to this prefab and play the sound when this game object is instantiated. This Asteroid game object was created at the start of game so when the player shoots this laser, the asteroid will explode and the enemies will start spawning.

On the explosion prefab game object, let’s add our audio source component and drag in the explosion_sound clip. We are going to leave ‘Play on Awake’ ticked as the second this explosion prefab is instantiated we also want the sound to be played.

Adding Code to Our Asteroid and Explosion Scripts

So within our “Asteroid” script let’s create a serialized game object (so we can see in the Inspector and drag the prefab into). Then, once the player has shot the laser into the asteroid, instantiate the explosion prefab. This explosion prefab has an exploding animation attached to it, along with an explosion audio clip and it will destroy itself once the animation has finished playing.

Then within our “Explosion” script that is attached the explosion prefab:

Let’s create a couple handle’s to the Animator and the AudioSource which we can then GetComponent. Once we have done that, run the StartExplosion() method which is going to trigger the explosion animation to play and then destroy itself once the animation has finished playing (2.633seconds). For our audio source we have set this so that when the script is called it automatically plays the sound on awake and doesn’t loop.

Hopefully this has helped you get some audio clips playing in your game!

--

--