OnTriggerExit: Can you trigger it by destroying an object/disabling a collider?

Hello, I have an issue. I’ve set up two objects with trigger colliders: Object A has a rigidbody, Object B does not have a rigidbody, but has OnTriggerEnter and OnTriggerExit functions.

When I move Object A into Object B, the OnTriggerEnter function always triggers. When I move Object A out of Object B, the OnTriggerExit function triggers, so everything seems to be working as it should.

My problem comes whenever I try to destroy Object A whenever it is inside Object B. The OnTriggerExit does not trigger. Neither does it trigger if I try to just disable Object A’s collider or delete its rigidbody.

I’m trying to stay really light on script usage, so I basically just want to use a List on Object B to keep track when another object enters into it, then OnTriggerExit to remove that object. But the problem is that an object may be destroyed/disabled while inside Object B… and I was hoping that OnTriggerExit would fulfill my needs without having to use something like OnTriggerStay or another List on the other object or some other check that runs every (Fixed)Update/frame.

Am I missing something? Or is there a simple way to make OnTriggerExit activate just through destroying/disabling an object/collider?

Thank you! :slight_smile:

Uh, a simple but hacky way to do it is to just set the object’s position to some arbitrary number right before you destroy it, but that’s probably not what you want to do.

There are workarounds to this for specific cases, like whenever you access your List you can just ignore and remove any objects which are null so you don’t get NullReferenceErrors, but it depends on the situation if that’s what you need or not.

1 Like

Thank you. I ended up “auditing” the list at the specific time that I needed the information to be current. It seems to work :slight_smile:

Ah! One thing I just thought of, but you could Subscribe to an Action whenever you do OnTriggerEnter, and Unsubscribe when it leaves. The Subscription would remove the GameObject from whatever list you’re using, and be called in the OnDestroy() method. Think that’s the most sane way to do it.

1 Like