I’m trying to detect when objects have left a collider when they are destroyed/disabled, so that I can stop attacking that object, as well as removing it from a collection it is stored in.
The documentation says "OnTriggerExit is called when the Collider other has stopped touching the trigger."
, understandably confusing when thinking about a disabled object. I see threads ranging back 4 years asking about this issue. It seems the acceptable workaround is to move your object up a few thousand world units, this probably works, but it seems like a hacky solution to something that should be more elegant.
Can I somehow call OnTriggerExit from the collider(s) the object is currently in when it is disabled? Or a way to get OnTriggerExit to be called automatically when an object is disabled?
I think it is ridiculous that the OnTriggerExit is not called when the component is disabled. It should be called right before the object is disabled so that it can be properly handled by other objects that need to know what the heck they are touching. So basically it should work like this.
gameObject.SetActive(false); → OnTriggerExit() { … } → OnDisable() { … } → object is now disabled
I don’t understand why this is so hard. The best solution really is to move the freaken object out to the edge of space and then disable it.
When you destroy the object or disable the object, the components on the object won’t work which includes the script components too. So you’ll have to make sure that the object stays alive for the collision trigger.
One way is to disable the renderer, so the scripts and colliders are still working but the object won’t be displayed. You can destroy the object when you no longer need it. Like OnTriggerExit.
Another way is to instantiate another empty object with the same colliders and script components at the place of the object you destroyed.