- Home /
Why is OnTriggerExit2D called before OnTriggerEnter2D?
I have a scene with lots of 2D colliders (aircraft) flying about, that sometimes get destroyed.
I have a trigger CircleCollider2D
that keeps track of all the aircraft that have entered and exited its collider.
Very rarely (less than 1 in 1000) the aircraft collider is destroyed, triggering the OnTriggerExit2D
callback slightly before the OnTriggerEnter2D
callback:
08:50:16.942-TargetDetector: OnTriggerExit2D() collider id: -31498
08:50:16.949-TargetDetector: OnTriggerEnter2D() collider id: -31498
Has anyone else run into this? Am I doing something stupid?
I'm using Unity 5.6.1f1.
Answer by felixmann · Aug 24, 2017 at 02:22 AM
My work around is to ignore OnTriggeredExit2D
callbacks for colliders I have not had a preceding OnTriggeredEnter2D
, and OnTriggeredEnter2D
callbacks for colliders whose game object is already destroyed (as they will never receive a subsequent OnTriggeredExit2D
callback).
This seems like an ugly hack, so any suggestions are welcome :)