Unity Dev Blog: How to Play Sound Effects in Unity Through C# Scripts
Adding in laser fire sound effects, explosion sound effects, and more!
Getting Unity to play a sound clip on demand is a little more involved than adding in the background music was. The process is generally the same; we still need an Audio Source on the game object where the sound will be played, but we also need a few lines of code to tell Unity when to play the audio clip.
In the Space Shooter 2D game, I want to add laser fire sound effects. In other words, each time the player fires a laser, the game says “PEW!”.
So let’s get to work! First, we need to add the variables to hold the reference to the Audio Source and the sound clip in the C# code. In the Start() function, I stored a reference to the Audio Source in the _audioSource variable. I did a quick null check and then assigned the reference to the audio clip to the .clip attribute of the audio source. After saving the script and going back to Unity, we can insert the clip in the Inspector window.
Remember — it is always a good idea to null check!
Next I need to add the Audio Component to the player game object and add the reference to the laser_shot audio clip from my Assets/Audio folder.
In the image above, you can see that I have added an Audio Source component to the player game object. I turned off Play On Awake and Loop because I want to control (in the code) when the sound clip plays and I only want it to play once each time. I have also added the reference to the laser_shot clip in the variable slot for the Laser Audio Clip.
Okay. Everything is set up and now all we have to do is play the clip when the player fires a laser.
By adding the line _audioSource.Play() to the FireLaser() function, the sound effect for the lasers will play as soon as the laser (either a normal or a triple-shot) is instantiated.
Next, we’ll add the explosion sound effects to the game. The explosion that is seen when the asteroid explodes and when the player’s ship explodes is a prefab game object that we made many articles back. SO here, to me at least, it makes since to just add the sound effect to the explosion game object and have it play as soon as the explosion object is instantiated.
Here, I have the explosion prefab game object selected and I have added an Audio Source component to it. I drug over the explosion_sound clip and dropped it into the AudioClip slot and turn off Loop. I left Play On Awake checked because we want the clip to play as soon as the game object is brought into the scene. Doing it this way means we do not have to write any C# code to get the clip to play.
Again, you cannot hear it in the gif but you can see the Audio Source icon and you can hear the explosion sound in game play. Let’s see if this one works better…
In this article I have covered two different ways to add sound effect to a game using Unity. You can add the needed components and reference the sound clips in your code for more control, or you can attach them to simple objects like our explosion here and just let Unity take care of playing the clip when the object is brought into the scene.
That’s all well and good, you say, but I want to hear it! Fine. In tomorrow’s article I will show you how to build and test a game in Unity. I will also show the steps for uploading your game to a web site so you can share a playable link to friends and family (or you can keep it as you own little secret).
See you there!