Powerup Sound Effect in Unity

Thomas Mauro
4 min readMay 29, 2022

--

It works but code seems to be required to adjust powerup sound effects. I will get to it. Explosions are too loud for the enemy. Adjusted and made another vid to see the difference.
Better, but still require the powerups to be louder via some code for sure and the laser is too loud. Cool for now.

I have the background music, laser, and explosion sound effects all working. The powerup is the last sound effect for what we currently have in this project. Technically every powerup can and perhaps should have its own unique sound effect. I plan that later, for now, it's about creating a workable foundation. Setting up the ability for the sound effect to work is my purpose here. From the background to the various sound effects there were differences in implementation. I was going at it like everything should be very similar. Similar in some aspects for sure, but not exactly. I will gladly do this article to keep track of my progress and go back to it for reference.

  • Add Audio Source component to all active powerups
  • Set the powerup sound effect in all of the active powerup Audio Source component AudioClip slots.
  • Play on Awake unchecked (set to false)
Get those Audio Source components prepped.

Cool cool, now time to make it work through code! The powerup sound effect is ready to be used therefore I need to let it know when it should play the sound effect.

  • Get into the PowerUp script to see where this should start
  • VARIABLE and a handle for the variable of course. I am still getting the Audio Source component so…
  • // Variable is private AudioSource _auidioSource;
  • // Assign to void Start and Null Check
  • // Call the sound effect when the powerup is collected by the player audioSource.Play();
Variable/Variable assigned/Null check/Call effect where it should be called

Cool, and NOPE lol. When testing the sound effect is not playing when the powerups are collected. It appears to play in unity, but there is no sound. The reason discovered was that in the same frame as calling the sound effect the object is also being destroyed at the same time. The sound effect is effectively being deleted in that instance therefore unable to play the sound. This also means audio.Play cannot work for this. Well, gameDevHQ is amazing when it comes to finding resources, and Unity documentation is out there. Back to a vital resource in this case to find another way to make this work for powerups. So thas step 1.

Under Public Methods in the AudioSource documentation and also looking into random solutions found by others on the web it seems a mistake is indeed made here. AWESOME! Time to learn more. I am using a variable to get the AudioSource component. For the powerups, we must go back to the start and make a different variable. A variable to call the audio clip.

  • Delete the current variable type AudioSource _audioSource
  • // Make a Variable to get AudioClip component private AuidioClip _audioClip; [Serialize] this Variable
  • Remove the entire void Start as it is not needed
  • Remove the AudioSource component from all powerups in the inspector
  • Of course save that script, now there will be an AudioClip slop on the powerup script components of all powerups. Assign via inspector
  • // Play the clip at the time of being destroyed and at the position of the powerup. Here is actually where the AudioSource needs to be called
Turns out that is all I need in the powerup script
Remove the AudioSource component from all powerups, and put the clips in all slots.
Play the clip at the time of being destroyed and at the position of the powerup.

Well, now I will see if this works. Pretty sure it will. At the very least there is a good foundation that works and can be built on. Next, I will look into deployment options and how they work. Not long after that add some extra stuff to this Galaxy Shooter. 3D will eventually follow. Let's roll! Oh, and I decided to get different power-up sound effects for each power-up after all plus changed the music on the main menu and game. Check it out. Also, the sound effects seem low so I need to look into it. You can hear them but the background music is too loud. I will figure that out in the vid at the top. Peace out.

--

--