- Home /
OnTriggerEnter2D being called, but not OnTriggerExit2D?
I want to increment the player's score when they successfully clear a box. To do this, I have one collider on the box for collisions, and one trigger over the box to see if the player is on top of it.
I have this script attached to the player to increment the score:
public int score = 0;
void OnTriggerExit2D(Collider2D col){
if (col.gameObject.tag == "Obstacle")
score++;
}
OnTriggerExit2D does not get called in this script, but if I change it to OnTriggerEnter2D it does. I cannot use that, because it constantly increments the score while the player is in the trigger. How can I fix this?
I'm unfamiliar with the 2D handlers, but the normal OnTriggerEnter should only be called once, when you first enter the trigger, not repeatedly while you are inside it.
But for the sake of a quick workaround, just keep a variable pointer to the last entered obstacle. Then check to see if you've already passed that obstacle before incrementing again.
Hope this helps!
Answer by Kilrath81 · Dec 15, 2013 at 08:24 PM
the 2d trigger stuff seems to be having issues. one way around it would be to have a empty game object with a boxcollider2d as a trigger just above and do an oncollisionenter2d for that tag to do your score.
Answer by small-U · Mar 09, 2014 at 06:43 AM
can move GameObject(MoveObject) add Rigidbody2D component and Box Collider2D, other one GameObject(targetObject) add Box Collider2D setting [isTrigger true] look my pictures..
the Test4 script code:
void OnTriggerEnter2D(Collider2D collisionInfo) {
Debug.LogError(" OnTriggerEnter2D ");
}
void OnTriggerExit2D(Collider2D collisionInfo) {
Debug.LogError(" OnTriggerExit2D ");
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613124228im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to add to a 3D object's length through code? 1 Answer
Overlap point of 2 kinematic colliders 1 Answer
How to add the right amount of points when my player destroys a gameobject? 0 Answers
Receiving information about getting hit by a Raycast 2 Answers
How do you execute Trigger-collider collision only in one gameobject? 1 Answer