Help with OnTriggerExit never being called?

Hi all!
I’ve been working on my first little game for a while, learning Unity and programming along the way. In this game, I have doors that open and close when a ‘button’ is stood on.

This is achieved using events (the button shoots out the events, which the door subscribes to):

public delegate void Simple();
public event Simple OnPressed = delegate {};
public event Simple OnReleased = delegate {};

void OnTriggerEnter(Collider col)
    {
		OnPressed();
    }

void OnTriggerExit(Collider col)
    {
		OnReleased();
    }

The door then listens to these events, and opens and closes appropriately.

The problem comes when objects standing on the ‘button’ are destroyed. As the destroyed object never exits the trigger, the function to close the door is never called and therefore remains open.

Surely there must be a better way of going about this entire door trigger system. As I plan on using a similar method on other objects in the game, I’d rather change my entire system sooner rather than later, or learn a good way of working around the problem.

To clarify my goal:
I wish to have a button that opens a door when stood on, and closes it when no object is on it.

Any suggestions are welcome, and thanks in advance!

Hey,

Why not use “On Destroy” which runs just before an object is destroyed. Use it with the objects which might/are be(ing) destroyed in your game after stepping on that button.

More info here: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDestroy.html

Leave the current code for live objects, which aren’t going to be destroyed, and add this to objects which you know are gonna be destroyed when pressing that button. ( just use the same code: OnReleased(); ) which you’re calling now, in the “on destroy” area of these objects.

Questions/Clarifications? Just write a note! :smiley: