Find all audioSources check if they are playing

Hello i need some help… im making a game for android and i have several enemies that fire a weapon at same time and play a sound. Now for perfomance i would like to check all the audioSources associated with this enemies and play only one of them or maybe two when the others are not playing…

allAudioSources = FindObjectsOfType(AudioSource) as AudioSource;

Probably the strongest choice here, without coding a full-fledged audio manager class…

It is often useful to keep a collection of living entities; actors, props, anything you ever want to access or track dynamically. You can iterate over the collection to make queries or changes. Add such objects to a collection when they get created and remove them when destroyed. The collection should be a property of a singleton so you can access it anywhere.

In this case, when you request the sound, for each actor in the collection of living actors, if the actor’s weapon’s audiosource is playing, increment a counter of how many concurrent instances are playing, and reject the play request if the total count is greater than some number.

With an audio manager class, you’d create a centralized approach, whereas it sounds like yours is completely decentralized. A centralized approach is much more convenient for dealing with “group decision” situations like this, but has its drawbacks too.