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.
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.
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.