Mute/Unmute with same trigger

Hello!

Trying to mute and unmute audio with the player.

The idea is to unmute the audio in a room when the player enter a trigger at the door, and mute when he exit the room with the same trigger.

What am i doing wrong?
CODE

using UnityEngine;
using System.Collections;

public class AmbientTrigger : MonoBehaviour
    {
        public AudioSource Ambient1;
        public AudioSource Ambient2;

    void Start ()  
    {
        Ambient1.mute = true;
        Ambient2.mute = true;
    }       

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            Ambient1.mute = false;
            Ambient2.mute = false;
        }
   
        if (Ambient1.mute = false)
        {
            Ambient1.mute = true;
            Ambient2.mute = true;
        }
    }
}

use :

if(other.gameObject.compareTag ("Player")) {
   Ambient1.mute = !Ambient1.mute;
   Ambient2.mute = !Ambient2.mute;
   }

You had a few problems with your current code:

  1. the player tag was only checked for the unmuting
  2. your statment : if(Ambient1.mute = false) was assigning (re: you needed 2 ‘=’ but only had 1)
  3. the first part set them false, so the next if statement, had it been written correctly, would have evaluated to true; you could have used “else” instead.

Ah i see! Thanks for your reply!
“Ambient1.mute = !Ambient1.mute;” is what i did before, but i had some errors and must have done some wrong with the code.
Now i works as it should do, thanks a lot!

You’re welcome :slight_smile:

Hmm…you can trick the collider to play and stop by just tocuh it, witch means you can make the audio play when you leave the room if you want.

So im gonna have 1 On and 1 Off trigger

But im not sure what im doing wrong this time, both On/Off colliders are triggers.
And i have done the script like this now

CODE:

public class AmbientTrigger : MonoBehaviour
    {
    public AudioSource Ambient1;
    public AudioSource Ambient2;

    public GameObject TriggerOn;
    public GameObject TriggerOff;

    void Start ()  
    {
        Ambient1.mute = true;
        Ambient2.mute = true;
    }       

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            if (TriggerOn)
            {
                Ambient1.mute = false;
                Ambient2.mute = false;
            }
        }
    }
}

I was aware of that “issue” you might face… hm…
Let’s see, so just have the one trigger on the more “inner side” always turn the sound on/mute off, and the more “outter portion” always turn it off? Make sure there’s a bit of space in between them, I guess, for good measure.

Yeah i have the On Trigger just a little inside the room in front of the door, And the Off trigger on the outside, the distance is enough so a player fits between the triggers.
But my code do not work, it doesnt unmute the audio

Well, I don’t know about the GameObject triggeron /Off…
That isn’t really required. If you really want to just have 1 script that you re-use, make a bool for On/Off - that’d be even easier.
Just change the code so if the tag is the player, mute = bool_variable
One will always turn it on and one always off.

Im trying to by manual clicking the bool mute the audio, but the bool keep getting checked on startand cant be unchecked.
What stupid error am i doing now?

CODE:

public class AmbientTrigger : MonoBehaviour
    {
    public AudioSource Ambient1;
    public AudioSource Ambient2;

    public bool MuteAudio;


    void Update () 
    {
        if (MuteAudio = true)
        {
            Ambient1.mute = true;
            Ambient2.mute = true;
        }
    }
}

Now you’ve changed it to Update instead of trigger. I don’t think that’s needed (or good for your situation)
You’re also assigning MuteAudio = true instead of == true

But going back to a previous post… I think you should have this:

public bool MuteAudio = false; // you'll have 2 scripts/objects and in the inspector of the one y ou want true, change this in the inspector

void OnTriggerEnter(Collider other) {
   if(other.gameObject.CompareTag("Player") {
      Ambient1.mute = MuteAudio;
      Ambient2.mute = MuteAudio;
      }
   }

Just start the game with them muted (or set .mute = true) in Awake() - either way.
This setup will mean anytime they enter the trigger for muting, it will always mute - doesn’t care if it’s already muted or not… and if it’s the other trigger it will always unmute - again, regardless of whether it was or not before.
But the effect should be what you want. :slight_smile:

So i just put this script on each trigger then, but i have a error

if (other.gameObject.CompareTag == "Player")
error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer

“tag” works fine then CompareTag

tested This one all of my rooms i have, works just perfect! thanks a lot! :smile:

it’s not CompareTag == “Player”

it’s CompareTag(“Player”)

You’re calling a function, not comparing a property.

CompareTag works like: .CompareTag(“Player”) not CompareTag ==
:slight_smile:

Note: CompareTag is preferred to ‘==’ when comparing tags. No garbage, as I recall reading/learning. So I try to point this out to people :slight_smile:

Yes, it’s garbage free, since the ‘tag’ property requires creating a string that is passed from the C++ side. Where as passing a constant string from mono to the C++ creates no garbage since the C++ side is self managed.

Cool, I knew it was something like that - but hadn’t remembered the exact reason :wink:

Ops…that was my fault…idk why i typed it like that :stuck_out_tongue: