- Home /
OnTriggerEnter2D not working, alternatives?
I've read that OnTriggerEnter2D has some issues, so I was wondering if there was another way to achieve what I need: getting the level to end after the 2 players are on their respective flag (there is no way one can get to the other's flag), here's what I had:
private int flag = 0;
void OnTriggerEnter2D(Collider2D col){
if (col.gameObject.tag == "Player") {
flag = flag + 1;
Debug.Log ("Flagged = " + flag);
}
}
void OnTriggerExit2D(Collider2D col){
if (col.gameObject.tag == "Player") {
flag -= 1;
Debug.Log ("Unflaged" + flag);
}
}
// Update is called once per frame
void Update () {
if (flag == 2) {
Debug.Log ("Level finished");
Application.LoadLevel ("1");
}
}
Each player is on a different layer but on the same tag (Player) because it's impossible for them to get to the other's space. The console prints
onTriggerEnterEvent: Goals UnityEngine.Debug:Log(Object)
but it won't add anything to the variable flag.
Is there other way to do this? or is this the correct way but I'm doing something wrong?
Wait - where is the flag variable declared? If you have two flag GameObjects, and flag is declared as part of their object, then you'll have two instances of flag, neither of which can ever reach 2.
Oh, I thought I had coppied it as well, it's declared before OnTriggerEnter2D as a private int, initialized in 0 $$anonymous$$mmh it's there, but for some reason it got out of the Code thing, sorry, I'll fix that
Well, I figured. But I don't see how the flag variable can ever reach two, since each flag object will have its own flag variable, so each one will only ever reach up to one. $$anonymous$$aybe if you declared it as a static.
even if the script is in the GameObject that has the 2 triggers? I mean, the triggers aren't on each flag/goal, they're on an empty GameObject parent of the 2 flags. Anyway, I tried it and it didn't work, and if I set it as public (to see the value) it never get to 1 either.
Answer by opponent019 · Apr 23, 2014 at 10:30 PM
Oh! this fixed it! Thanks Pyrian for trying to help me anyway :) http://answers.unity3d.com/questions/592567/why-isnt-my-ontriggerenter2d-function-working.html
Interesting. The tutorials talk about the fact that at least one of the colliding objects must have a RigidBody2D - they gloss over the fact that it must not be kinematic! Seems dumb to enable force-free kinematics just to get a trigger to fire, but it DOES work with a $$anonymous$$imum of fuss. I wonder what the performance cost is, though? $$anonymous$$ight be better to do something like Physics2D.OverlapArea, although it'd be a pile of work to do that for a complicated collider. Ugh. Thanks for the tip, sorry I couldn't help!
Answer by Pyrian · Apr 22, 2014 at 04:14 AM
Ohhh, that's your problem. Triggers won't get called in parent object scripts. You have to have the script catching the trigger as a component of the exact same GameObject where the Trigger Collider actually is.
So, what's your next step? Probably the easiest solution is to move that script into each of the children, while making the flag a static so that it's value is shared between them.
Oh no no, sorry English is my second language so I can't explain myself very well; this GameObject (Goals) is parent of 2 objects: goal1 and goal2, Goals has 2 box colliders that are triggers, each positioned where the children are, but the colliders are in Goals, not each little goal; the script (Flagged) is in Goals as well
Your answer
Follow this Question
Related Questions
OnTriggerEnter detection too late 0 Answers
Trigger only one collider? 1 Answer
Ontriggerstay2d only runs 26 times in a row? 3 Answers
How can i detect hits from enemies inside of a player? 1 Answer
OnTriggerEnter2D Doesn't work? ;_; 4 Answers