OnCollisionExit not working when GameObject vanishes

Hey! So I’m pushing a GameObject into another with a Rigidbody2D+collider. I have a OnCollisionExit2D on the Rigidbody object. I ran into problem where OnCollisionExit2D is not working when a GameObject gets disabled. The code doesn’t register the GameObject vanishing as an exiting collision for some reason. Is there any way around this? Thank you.

A gameobject needs to be active in order for it to interact with the physics engine, so disabling/destroying a gameobject means it will not called any OnTriggerExit/OnCollsionExit functions.

There is are OnDestroy() and OnDisable() functions which allow you to add in some logic just before a gameobject is destroyed/disabled. You can use them to manually handle whatever logic you need to.

A solution I used was to translate the object far out of the game world and wait for one physics frame (which causes OnCollisionExit to fire), then destroy the object.

–Eric

Thanks a lot for the tips and sorry for the late reply! I’ll have to try both in a few days.