Nerd For Tech
Published in

Nerd For Tech

How to Play Sound Effects in Unity

Now that we have some background music, let’s work on adding a couple sound effects to our game.

First, let’s get a laser sound effect going. To start, we are going to create an empty game object and rename it to Laser. From here, we will add an audio source component and then drag in our laser sound effect:

Now, we will have to go into our player script and add some code to allow our laser sound clip to play when we fire our laser. First, let’s create some game components for us to attach our sound effects to:

On top of the AudioClip for our lasersfx to play from, we will also create a connection to our Audio Source for the player. This will allow us to pull the sfx straight from the audio source rather than connecting it manually and having any possibly future glitches occur. From here, we can go into our Unity Editor and attach the laser to the Audio clip:

Finally, we will want to scroll down to our fire method and add our code to activate the laser sound clip. We are letting Unity know that when we fire a laser, that is when we want our sound to trigger:

Finally, we can test it in the game and what we will see is that the audio clip in the audio source for the player will switch to the lasersfx upon playing the game:

Next, let’s get some explosion effects for destroying our asteroid and enemy. First, let’s get the quicker one out of the way and add it to our asteroid. As for the method we will use, we will have to take a look at our asteroid script and see remember how we have it coded.

As we can see with the asteroid, we currently have it’s explosion effect running through a game object, and that object has it’s own script, so we can move over to our explosion script and add in the code we need to activate the explosion:

We simply will repeat the process we have earlier, and have getcomponent our AudioSource. In this case, we will apply the explosion sound effect directly to our audio source, so we do not need to create a component to link the sound effect to that we will have to reference in this script:

As for our enemy, we used a slightly difference method in creating the explosion. Instead of referencing to this explosion, we created the code within our enemy to explode there. Therefore, we need to repeat the process we just did with our asteroid and apply it to our enemy. We will need the add the AudioSource component to our enemy and attack the explosion sound clip to it. From there, we will create the link to the AudioSource game object within our code and tell Unity we want the audio to play both when the laser hits the enemy, and when the enemy hits the player:

As well, we will want to turn off the play on awake option within our audio source in the inspector so that we do not hear the enemy spawn in with explosions all the time:

Finally, let’s get a sound clip for our powerup upon collection. We would think that we can just follow the same method we have been using and just create a link to an audiosource that we have attached to all of our powerups and just call that audio clip, however that method will not work this time. This is because what is happening is our powerup is being destroyed the moment the player collects it, which destroys the sound clip with it. To remedy this, we have to use a different method of playing an audio clip. First, we have to figure out what type of method we need to use, and when we have to research what to do, google is always a good friend of ours. After searching for answers through forums, we will notice a reference to PlayClipAtPoint, so let’s check out what that is all about in our scripting API:

As we can see, the play clip at point will allow us to instantiate our audio clip at a specified location, essentially creating an empty object at that point to play the audio clip for us, then it goes away. So, with this knowledge, we can go back to our powerup script and apply it there:

In our script, we set our clip and a private AudioClip and attach our audio clip to it through our inspector. Within our script, we will tell Unity that when our powerup interacts with our player, we want it to play our clip at that location.

There we have it. We have successfully used 3 difference methods to attach our sound effects to some objects in the game. Now that we have finished this up, we can look at how to deploy our game, along with adding in some new behaviors within our game.




NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit

Recommended from Medium

CI/CD with Jenkins — Part 1: Install Jenkins on Ubuntu

What will it Cost to Build a Taxi-Booking App?

What will it cost to build a taxi-booking app?

Understand the Powerful ROP Attack from Zero!

Implementing a FHIR healthcare API

Try Chef Automate on Elastic Compute Service

How I built my own Linux System

Uipath — Hands-On

Tap Tap APK download for PUBG | Tap Tap PUBG

Tap Tap APK download for PUBG | Tap Tap PUBG

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jordan Evans

Jordan Evans

More from Medium

Basics of Optimization in Unity

For Loops | C#

Adding proximity detection so enemies can ram player

Setting up git with Unity