How to change volume on many audio objects with specific tag ?

Hi guys !
I’m creating game, and I want to make switches to mute SFX and Music, all audio objects in my game tagged with “sfx” and with “music” tags, and I want to mute them separately.

Waiting for help ! Thanks in advance !

The following solution does not need the use of tags.

AudioListener.volume controls the global volume. This is good for SFX because you can have different internal volume on each sound + a master volume.

But you want to control the BGM volume separately. The best way is to make the AudioSource with the music AudioClip independent from the global volume using ignoreListenerVolume.

audio.ignoreListenerVolume = true;

Therefore, to mute all SFX, just set the master volume to zero. By doing this way all SFX keep their internal volume, and the BGM volume is not affected.

AudioListener.volume = 0;

On the other hand, to mute the BGM you just need to set its own volume to zero (or set mute to true).

// setting volume to zero
audio.volume = 0;
// or mute
audio.mute = true;