OntriggerEnter condition with two colliders

Hello,unity beginner here. i’m making a “escape room” game where you have to find the 5 numbers across the room to be able to leave it. One of the numbers appears when the player finds all the chairs and put them with the table, problem is, i don’t know how to use the OntriggerEnter with more than one object. I don’t know if i’m explaining myself, with the code i think it will become clear. I have this:

    public GameObject handNumber;

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Chair")
        {

            handNumber.SetActive(true);
        }
        
    }
}

That works ok, but i have multiple chairs so it would be something like:

    public GameObject handNumber;

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Chair" && "Chair2")
        {

            handNumber.SetActive(true);
        }
        
    }
}

But that doesn’t work. Could you help me? Thank you so much!

Whenever you work with stuff like this, it’s always best to temporarily store the information you want to compare something with/against.

  1. every time a chair is placed in the designated area, increase an integer count

  2. add a “chair_X_has_been_triggered” or something to ensure trigger only activates once

  3. when the integer total is X - do something

    public GameObject handNumber;

    // has Chair 1 been placed near table? etc
    public bool Chair1Placed;
    public bool Chair2Placed;
    public bool Chair3Placed;

    // how many chairs have been placed?
    public int TotalChairsPlaced = 0;

    void onTriggerEnter(Collider other)
    {
    // if Chair1 is placed in trigger box
    // and it hasn’t been placed here before
    // increase the Total Chair count by 1
    if (other.tag == “Chair” && Chair1Placed == false)
    {
    TotalChairsPlaced += 1;
    Chair1Placed = true;
    }

     if (other.tag == "Chair2" && Chair2Placed == false)
     {
         TotalChairsPlaced += 1;
         Chair2Placed = true;
     }
    
     if (other.tag == "Chair3" && Chair3Placed == false)
     {
         TotalChairsPlaced += 1;
         Chair3Placed = true;
     }
    

    }

    void Update()
    {
    if (TotalChairsPlaced == 5)
    {
    handNumber.SetActive(true);
    }
    }

Thanks man! That worked like a charm =)

Hi,

When you are programming with C# in Unity, you are actually writing C# code, with its all functionality.

In the if statement in your code, you are making a String comparison, checking if the collider entered into trigger area has the same tag as “Chair”.

You can also use CompareTag("your game object tag") function to compare this.

Also, && is actually means that if and if, but where a game object cannot have more than one tag, so I assume you want to check if or if, using ||

So your code becomes:

 public GameObject handNumber;
 
void OnTriggerEnter( Collider other) {
    if ( other.CompareTag("Chair") || other.CompareTag("Chair2") ) {
        handNumber.SetActive(true);
    }
}

Mind that you are checking for game object tags. You can also compare for game object names.