Journal 29 — How to Play Sound Effects in Unity

Chris Nielsen
4 min readMay 16, 2021

--

In the previous Journal, we learned about adding audio to a game to play in the background. Now we want to add sound effects for:

— when lasers are fired

— when the Player is destroyed

— when the asteroid is destroyed at the beginning level to start the spawn waves

— when an enemy is destroyed

— when a power up is collected

To add sound effects to the laser, and the Player destroyed, we add references to the Audio Source component and an audio clip in the Player C# script.

Unity C# variables for audio clips and Audio Source

In Void Start, we add a reference to the Audio Source component, and we can assign the laser sound as the audio clip.

Unity C# reference to Audio Source and laser audio clip

In the Player script, when we hit the spacebar and fire the laser, we play the audio clip.

Unity C# play laser sound each shot

During Playmode, you can see the results where the laser is added to the Audio Source component.

Unity Playmode, laser audio clip added to Audio Source

In the Player script, where the player’s lives are checked at less than 1, we add a reference to play the audio clip, and also update the destroy command to delay by two seconds, in order to play the audio clip.

Unity C# player death, play explosion clip and delay Destroy call briefly

In the Player game object, we add the Audio Source component and drag the audio clips to the Inspector.

Unity Inspector for Player, audio updates

For the asteroid, we can add the sound to the explosion game object for when it’s instantiated. Similar to the Player script, we add a variable for the Audio Source component and audio clip to the Explosion script. In Void Start, we add a reference to the Audio Source and play the explosion sound.

Unity C# variables for audio and call for explosion sound in Start

In the Explosion prefab, we add the audio clip and Audio Source component.

Unity Inspector for Explosion prefab, audio updates

During testing, I found that if you rapidly fire your laser, you may hear multiple explosions on the asteroid, instead of just one. You can fix this by disabling the asteroid’s collider in its script, right after the explosion is called.

Unity C# for asteroid script, disable collider before it is destroyed

For the enemy explosions, we add similar code like the above examples to the Enemy script.

Unity C# script for Enemy, variables and call for audio
Unity C# Enemy death, play explosion audio
Unity Inspector for Enemy, audio updates

Finally, for the power up sounds, we can add the variables and reference to the Audio Source component and audio clip.

Unity C# updates for Powerup script, variables and call for audio

In the powerup script, we can play the audio clip when the Player touches the power up. In order for the power up audio clip to play completely, we can disable the sprite renderer and delay the destroy command briefly.

Unity C# update for Powerup, play power up sound and turn off renderer so sound plays before it’s destroyed

On each power up, we drag in the audio clip, and we attach the Audio Source component.

Unity Inspector for each power up game object, audio updates

After running through all these steps, all the sounds have been successfully tested. You should confirm that all the Audio Source components are attached to game objects, and make some adjustments to their respective volume settings to your liking, because some of these clips can come off very loud.

Thank you for stopping by!

--

--

Chris Nielsen
Chris Nielsen

Written by Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.

No responses yet