Day 19

Frankie Rodriguez
4 min readDec 8, 2020

--

Oh, the sweet sound of music.

Once I played around with the image settings, I worked on implementing audio into my game. First, I created an empty game object and titled it Audio_Manager. I then created a separate game object ‘Background’ for the background audio because this was not the only audio clip that was going to be included in my game. Next I added a Audio Source component to the Background gameobject. From here I dragged and dropped the Music_Backgoround clip from the audio folder to the Audio Source. To prevent the background audio from cutting off after a few seconds, I need to play it on an infinite loop . To do so, I turned the loop set to active.

Laser & Asteroid/Enemy Explosion

Before getting started, I searched up how to play specific audio clips and using what I read from Unity — Scripting API: AudioSource.Play (unity3d.com), this is how I will play the fire laser, asteroid/enemy explosion sounds.

First I let Unity know what we want to play. Here’s how I did it: Player > add component > audio source > open script to create variable (Visual Studios)> private AudioClip _laserSoundClip; & private AudioSource _audioSource; & [serializeField] for both. Back on Unity, under Player > drag and drop laser_shot to laser sound clip slot. From here I had two choices, either drag and drop the audio source into the audio source slot OR on Visual Studios get.compenent audio source to ensure it is never null. I chose the latter because if Unity ever crashes, I won’t have to reassign the audio back to their respective slots. Below is my code for the laser Audio Source (line 49).

Next, I null checked the audioSource using if(_audioSource ==null) { Debug.LogError(“AudioSource is null!”); } statement. In void Start I also assigned the audio clip for the laser shot using an else { _audioSource.clip = laserSoundClip; } statment — here I am able to get the clip or set the clip. Under the fire laser method, I want to play the laser audio clip. To do so, I accessed the audio source using the code — _audioSource.play(); — this will automatically play the clip.

I coded the asteroid and enemy explosion using the same process. Instead ofplacing the asteroid and enemy audio codes under the fire laser method of the player C# script , I placed the asteroid explosion audio source under the asteroid C# script — if (_audioSource == null) { debug…….} else { _audioSource.clip = _asteroidExplosion; } and the enemy C# script for the enemy explosion — if (_audioSource == null) { Debug…….} else { _audioSource.clip = _enemyExplosion; }.

For those following along with the course work, you will notice that I didn’t exactly follow the step-by-step for the asteroid and enemy explosion. I wanted to see if I was able to code those audio clips on my own. So far, no problems.

Next, I went on to implement the power up audio clips. At first, second, and third time, I tried to figure it out on my own by using the laser audio process, with no luck. The problem I faced when trying to use the laser audio process was that my player was no longer able to interact with the powerups. So, I deleted the code method for the interaction and rewrote it, which worked, THANK GOD! Not wanting to completely screw up any of the codes I worked hard to set up, I followed along with the course video. One other reason why I am unable to use the same laser audio process for the power ups is that when the power up audio clip gets called, the game will automatically destroy the clip when the power up object is used/destroyed. To avoid this I searched how to destroy object and play sound? — Unity Answers. This is what I learned:

  • Do not destroy the object, just disable it and destroy it after a certain delay — here I can use a coroutine
  • Create a global object holding an audio source you will use to play the destroy sound — here I can use audio manager
  • Before destroying the object, Instantiate an empty gameObject, attach an audio source and make it play the sound. Major drawback here: many empties may be created, which will slow down your game.
  • Use AudioSource.PlayClipAtPoint static function — This function creates an audio source but automatically disposes of it once the clip has finished playing. (Unity — Scripting API: AudioSource.PlayClipAtPoint (unity3d.com))

Under the Powerup C# Script — Instead of a reference to audio source, I referenced the audio clip private audioClip _clip; [serializeField] — *if you started to code using the laser audio process as above be sure to delete all codes for audio source on Visual Studios and any audio source components added to the power ups.* Under the power up script inspector, I assigned the clip variable. I selected all of the 3 power up prefabs, dragged and dropped power_up_sound to the clip slot. Back on Visual Studios, I called the playclipatpoint when the power up game objects are destroyed using the code — AudioSouce.PlayCLipAtPoint(_clip, transform.position_;

With my fingers crossed I ran the game…..Huzzah! All of my audio is now intact and playing appropriately.

--

--