3
\$\begingroup\$

I'd like to have a button to mute game sounds and another to stop the game music. How can I do this?

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

In the past, I've attached a script to the music AudioSource that sets ignoreListenerVolume, to bypass the main volume controls.

A crude example to get the point across:

void Start() {
    AudioSource music = GetComponent<AudioSource>();
    music.ignoreListenerVolume = true;

    //you can manage the music's volume directly, now
    music.volume *= 0.5f;
}

To adjust SFX volume, change the master volume of the audio listener. To adjust music volume, change the volume for the music's audio source(s).

This doesn't take into account the new audio system that shipped with Unity 5. It may now be easier to manage this with audio groups and mixer controls.

\$\endgroup\$
0
\$\begingroup\$

One approach is to set ignoreListenerPause on the AudioSource for your music and then set a mute on the overall game using AudioListener.pause. Now AudioListener.pause will mute everything except your music, while you can still mute that audio source directly. I take that approach in the audio chapter of my book (see my profile).

\$\endgroup\$
-2
\$\begingroup\$

This is not directly possible in Unity because:

  1. There is no difference between sound and music for the Audio Sources
  2. The volume of every Audio Source is independent of eachother

You will need to search for the Audio Sources in your local scene to iterate through them with Object.FindObjectsOfType.

var audioSources = Object.FindObjectsOfType(typeof(AudioSource)) as AudioSource[];
foreach (var audioSource in audioSources)
    audioSource.volume = 0;

To distinguish between sound and music you can use tags and check them with audioSource.gameObject.CompareTag.

\$\endgroup\$
4
  • 3
    \$\begingroup\$ "This is not directly possible..." is not accurate. Using AudioMixers, it's possible to create SFX and Music audio groups, and control the volume of one group independently of the other. Rather than setting tags to distinguish the groups, you'd set their output parameters to point to the appropriate mixer group. See this tutorial: unity3d.com/learn/tutorials/modules/beginner/5-pre-order-beta/… \$\endgroup\$
    – DMGregory
    Jun 29, 2015 at 22:45
  • \$\begingroup\$ Thanks for the info, I did not know about this. \$\endgroup\$ Jun 30, 2015 at 5:26
  • 1
    \$\begingroup\$ @DMGregory You should write an own answer explaining how this works in detail. \$\endgroup\$
    – Philipp
    Jun 30, 2015 at 13:10
  • \$\begingroup\$ @Philipp Unfortunately I haven't actually used AudioMixers so I can't speak from experience what the best way to set them up would be. I just knew from Unity presentations that it was possible. I'm hoping someone with expertise in this area may be able to pick it up from here, but if it goes another day or so without then I'll write up what little I know. ;) \$\endgroup\$
    – DMGregory
    Jun 30, 2015 at 13:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .