120 Days to Master Unity (Day 12)

Brian Perry
4 min readMay 31, 2021

Adding video game sounds

Lets add some sound to our games. Like everything else, creating sounds in Unity is not all that hard. The basic setup to get sound working in Unity is to make Unity aware of the AudioSource clip you want to play by making a variable in a script and making it a serialized field, grabbing a reference to that clip by creating an AudioSource variable to hold a way to access that clip, and then using the Play() method to play the clip.

The first thing you want to do when attempting to create a sounds in your game is create a variable for the audio clip or clips. This has to be done in a C# script. In my game, I put all of the variables that will hold a reference to the audio clip into my Player script.

Player script setup

In the above C# script, I added AudioSource variables with the [SerializedField] decorator so that any audio clips in my assets can be dragged and dropped into the inspector for my Player script as shown below.

Creating an audio clip game object

All I had to do is type in the variables above using [SerializedField], since all the variables are private, so I could drag and drop all the variables on the left into the Audio Field variables on the right that are highlighted.

Once I have all of the variables dragged over, I now have access to the sound clips I just dragged over in whatever script in which I created the variables. In my case, I created all of my variables in my Player script. Since I created all of my Audio Source variables here, I now have access to them in my player script.

The next thing needed to create audio in my Unity game is a game object. In Unity, everything is usually in some sort of component inside of a game object. If you want to play audio, you need to create an audio source component inside of a game object. In my case, I put my audio source components inside of their own game objects inside of player. I could put these game objects anywhere. I didn’t have to make them child game objects to the Player game object but that is what I decided to do.

Audio game objects

Since I made separate game objects for my audio clip sounds, I can now put my audio clip game objects inside of the audio source variables I created earlier. I could have put the audio clips inside of my Player script but I thought it would be a lot more organized and a lot more logical to put my clips in their own game objects and then just drop them into the audio clip variables I created for my Player script.

Dragging audio clip game objects to Player script variables

I think doing things this way made it a lot more easier to organize. I can now easily use these clips anywhere in the project. For instance, I can use the explosion sound for any game object in the project like Asteroid, Enemy, or Player scripts. In my player script, I have a variable for the LaserShot sound and the explosion sound so that when the player shoots, there is a laser shooting sound any time the player shoots a laser.

Getting a reference to the laser and explosion audio clips

Since all the audio sounds are in their own game objects, I can access them anywhere in the project by typing GameObject.Find(“AudioClipName”).GetComponent<AudioSource>(). The audio source variable will then be able to use the Play() method to play that particular audio clip like shown below.

Playing the Laser shot sound

In the above C# script, I can play the _laserShot AudioSource variable defined in Start() by just calling the Play() method on _laserShot. This makes it very easy to use anywhere. If I want any particular sound, all I have to do is use Find and GetComponent to get a reference or handle to that sound. In the above script, I use Play() to play the _lasershot sound. Later on, I could even create a special tripleshot sound for the tripleshot powerup.

So now with all of this logic in place, sounds for all of the game objects can be created and played.

--

--