OnTriggerEnter fires but OnTriggerExit does not

Unity_CNJ7zOsM7Q
“-1324” is green road, “-68908” is white road

// paste code here


This script is on both green road and white road

The green is generated road with mesh, collider, rigid body. When I move my cursor on the GIF one end of the road collides with another road (white) and OnTriggerEnter fires on both roads script, which is fine. However when green road leaves collider of white road there’s is no OnTriggerExit on both of them! It fires though if I move the whole green road object.

Why this happens and what could be a workaround?

OnTriggerExit won’t be called if the collider is disabled or destroyed.

It’s not obvious from your video but it seems first the road and its collider is constantly re-generated? Likely the collider is destroyed (in your script or internally in Unity) and therefore never triggers a OnTriggerExit.

You’ll have to work around this somehow. E.g. move the collider before destroying it (you’ll have to wait for the next physics update for the separation to be processed) or keep track of all collisions and manually call OnTriggerExit when you disable or destroy a collider (there’s some discussion about this in this forum thread).