Play Sound Effect in Unity- Game Dev Series 28

S.J. Jason Liu
3 min readMay 23, 2021

Objective: Create sound effect of explosion, laser, and powerup effect.

We’ve got background music in the previous part.
Let’s add some other sound effect to our game!


To create laser sound effect, we need to locate the code that generate the laser prefab, which is in Player script.
Create 2 variables to storage the audio clip and the audio source. With audio clip, make it SerializeField we could attach it in the inspector. And call the audio source component in the Start().

Add a debug statement and select the audio clip with code

Back to Unity, drag the laser audio to AudioClip, then add an AudioSource component.

Back to script, inside the ShootLaser(), add a line to play the audio source.

Play the game, and you should hear the laser sound when you fire now.


We need to change more stuff in powerup sound effect.
Open Powerup script, create 2 variables and GetComponent in Start() just like what we do in Player script.

Down to OnTriggerEnter2D(), we will need to play the audio when it collide with player, therefore we need to add the line before the powerup effect active.

Play audio before the if statement

Back to Unity, multi select all the powerups in the prefab folder, and attach the powerup audio effect file, then add a component.

Play the game and you will notice that powerup audio will not be played.
That is because we destroy the powerup gameobject before the audio play. To fix this, we will need to delay the destroy command.

However, the player might still get the chance to collide with this powerup within this second. We need to disable 2 component when collided, BoxCollider2D(or CircleCollider) and SpriteRenderer.

Play again and you should see it happens as your wish.


There’re 2 ways of explosion in our game, asteroid(which is as same as Player) and enemy.
Asteroid(or Player) is quite simple. Since the explosion is generate from another prefab, we just need to add an audio source to that prefab and it will automatically play it when generated.

Click Explosion prefab from Project folder, and add an Audio Source component to it. Drag the explosion audio clip to it and make sure the Play On Awake is checked, then it is done.

About enemy, we still need the same setting as in Player script.
Add 2 variables, Get component in Start() in Enemy scrpit.
Drag the clip in inspector and add a component in Unity Editor.

The timing we played audio is when enemy collide with laser or player, which are all with an animation of explosion. We just need to add the play command under the animation trigger.

Now you should have all the sound effect and functionable. Enjoy it!



S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.