- Home /
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!
Answer by winterfluxstudio · May 23, 2018 at 10:39 PM
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);
}
}
Answer by TanselAltinel · May 23, 2018 at 10:41 PM
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.
That will not work as it will enable the handNumber GameObject if any of the chairs are placed within the trigger. OP only wants to activate the GO when a certain number of chairs have been placed (for example, when 5 chairs are placed then handNumber.SetActive(true)
One of the numbers appears when the player finds all the chairs and put them with the table
"Work" as in won't work for the specific case asked. I thought it was better to show direction and let the op improve himself, but well.
It's called Unity Answers... not Unity Show Direction and Let the OP Improve Himself. I assume thats why you downvoted my answer even though it was correct. That's petty for someone with +1000 rep.
Your answer
Follow this Question
Related Questions
Can't call GetKey inside OnTriggerEnter? 2 Answers
OnTriggerEnter/Exit not working 1 Answer
When player enters triggerzone call function 2 Answers
Application.LoadLevel ("lower") loads on start-up, not when triggered; Deadline is soon HELP! 3 Answers
Trigger in child object calls OnTriggerEnter in parent object 3 Answers