Playing sound effects in Unity

Do you hear that? Of course you don’t, this is a text post that doesn’t make any sound. Be a lot cooler if it did.

Sounds are something that can really make a game stand out and become a great interactive experience. Background music is one of the best ways to increase the immersion in a game, so let’s add some to the space shooter!

I created an empty object to act as my sound manager in the game scene. I created a child for it called “Background” and attached an audio source component to it. I attached a music file to the Audio Clip element and made sure the “Play on Awake” and “Loop” boxes are checked. When the game starts, this object will start playing the background music and loop it.

In college, a group of friends made a similar style of game and used a recording of one of them saying “PEW!” as the sound effect for firing the weapon. The sound of Dayne saying “PEWPEWPEWPEWPEWPEW” still haunts my dreams and I refuse to subject my players to such torture. Maybe just a simple laser sound will suffice here.

I added an audio source to the player object but I’m not assigning it a clip to play because there are multiple sound the player object will be playing. I’ll use code to play the laser sound for now. I created some serialized variables for a laser audio clip and an audio manager object. I assigned the laser clip in the inspector and assigned the audio manager in the Start() method. I added a line to the firing method to play the sound once when firing: _audioSource.PlayOneShot(_laserSound);