if !audio.isPlaying for any object, not just this one.

Hi,

I am playing a sound when a player destroys blocks in a puzzle.

Sometimes the player will destroy one block, sometimes 5 blocks for example.

I had the code for playing the sound in with the destruction of the blocks, but this is giving me overlapping sounds, so 5 blocks are louder than 1 block etc.

I tried creating an AudioManager object to hold the AudioSource but I’m having some trouble with the script. It’s been pretty simple to code so that a block object knows when it’s been selected and when it is to be destroyed and having it play audio at the right time was pretty straight forward, but I can’t figure out how to get the AudioManager to check whether any blocks are being destroyed, and if any are, play the audio clip one time.

At the moment I can either have the audio play from within the block but then it will multiply and that’s a problem, or I can bind the audio clip to the trigger key, but then it will play when the trigger key is pressed even if nothing is being destroyed, which is a problem too.

Sorry if I made that over complicated, any tips would be appreciated.

Thanks.

You can either have an audio source for each block and keep a list and immediately stop the audio using AudioSource.Stop for any running instance upon launching a new sound (or at least start fading the others), or you can keep one audiosource and stop it once a call is issued to start the function.