How to mute specific audio sources?

How do I access the audio source of a specific object?

For example if I have a Bob object that wants to mute the audio source of a Steve object while continuing to play Bob objects audio source.

If your ‘Steve’ object has a unique name or a unique tag, you can do the following:

GameObject.Find("Steve").audio.mute = true;

Or with a unique tag:

GameObject.FindWithTag("Steve").audio.mute = true;

Given an AudioSource, you can stop it, pause it, or mute it.

To do that, you need to know which AudioSource you’re targeting. The simplest thing is usually to show a variable in the inspector, so that you can set it by hand.

You can also use GameObject.Find or FindWithTag and GetComponent to find the AudioSource.

This is a lot of reading, but understanding how to find other objects in the scene is crucial to developing games in Unity.

The most flexible and powerful way to do this would be with an audio mixer.

  1. Create an audio mixer asset in your project.
  2. Add two groups to the audio mixer named “Bob” and “Steve”.
  3. Assign audio mixer group “bob” to the output field of the audio source for Bob.
  4. Assign audio mixer group “Steve” to the output field of the audio source for Steve.

Now you can do all sorts of really advanced things through the audio mixer. Like decreasing the audio level of everything except Bob. This is a technique known as “audio ducking” and sounds exactly like what you want. Check out the official Audio Mixer and Audio Mixer Groups Tutorial to learn more.