How to play the destroy sound only if it isn't already playing?

So sometimes multiple blocks are being destroyed at the same time or in quick succession and then sound doubles increasing it’s volume. As we’re practically creating a new audio source for each destroy sound I don’t see a way I could detect that it’s already playing. What would be the preferred way of handling this scenario? My thought is to make a static audiosource and then play it only if it’s not already playing but I’m not sure what’s the proper Unity way of doing something like that.

What I usually do for my games is make an AudioController. Every other game object that wants to play sounds or music send their clip to the Controller and it decides how to deal with it. Then you can make a bunch of different public functions that can handle different things such as play sound only if not playing.

Hi Matija,

You could check the value of the isPlaying property of the AudioSource object. Also take a look at the AudioSource class in the API to figure out if there is something else you could use to fix the issue.


See also:

I can’t since the sound was made using AudioSource.PlayClipAtPoint which creates an audio source not bound to any gameObject and the method is void (which makes sense as the audiosource destroys itself after playing and having a stale reference to it would be asking for trouble). I could make a separate empty gameObject, add AudioSource to it and reference that object inside the block class to play the sound only if it’s not playing already. Is that the Unity way?

Depending on what your goal, you could use the lengths of the audio clip which you pass on to the PlayClipAtPoint method.

Alternatively, create a new game object like you said.

If your issue is that the volume increases if multiple audio sources are being played, please watch this tutorial. I currently don’t know if it works with AudioSources instantiated by PlayClipAtPoint but for normal AudioSources, it works.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms