Immersion Starts with Sound.

How to Play Sound Effects in Unity

Objective: Bring in some immersion to our game by adding in music and sounds.

Let’s start with something easy, the background music. Start by adding an Empty GameObject to hold all our different Audio Sources. Then make another Empty GameObject under the previous one we just made, to contain the background music.

Next, add an Audio Source component to the background music. Click the box beside the Loop option, this will keep the music playing while the game is playing.

Next, drag in the Audio Clip you want to play in the background. You can use .aif, .wav, .mp3 and .ogg file formats.

Now when you start up the game you should hear your background music playing.

Adding in the background is the easy part, playing a clip for say shooting a laser is a little bit harder.

Add an audio source to the player and open up the script.

Since we are wanting to change the Audio Source we will need a reference to the Audio Source. Next, we will need to use GetComponent for the Audio Source.

Remember to do those null checks!

Next, we will want to add the clip of the laser firing. Since we can’t just play the clip directly, but can through the audio source, we will need a reference to the clip so we can play it through the source.

Now we can set the clip for the audio source as the laser clip. we can do this by using an else statement after we check if the Audio Source is null.

Now to play the clip when we shoot the laser, head down to the shooting method we made a while ago, and play the audio source. To do this we can use “the variable for the Audio Source”.play();

After that, remember to test everything out.

Game Developer in the making~