LooMeenin
LooMeenin

Reputation: 818

How can I mute specific audio sources in Unity3D?

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.

Upvotes: 1

Views: 3278

Answers (1)

MLM
MLM

Reputation: 3688

If the object you want to find in the scene is named "Steve" then you can just use GameObject.Find.

GameObject steveGameObject = GameObject.Find("Steve");
if(steveGameObject != null)
{
    AudioSource steveAudio = steveGameObject.GetComponent<AudioSource>();
    if(steveAudio != null)
    {
        // Mute Steve's Audio
        steveAudio.mute = true; 
    }
}

If you already know how to get the gameobject or are getting it from a collision, raycast, etc, then you can just use the same code but with that gameobject instead.

Upvotes: 4

Related Questions