I have a mine with a large bounding sphere. When I enter the sphere, TriggerOnEnter works just fine. When I leave the sphere, Exit works fine. When I activate the mine and it explodes and I call Destroy, TriggerOnExit doesn’t activate until I leave the area of the sphere that shouldn’t exist. What am I missing?
This is the expected behavior I’m afraid. When a collider is removed it does not shrink to nothing, it just goes away. I’m surprised you get the Exit Trigger when you leave the area though. We didn’t.
Long story short, when you destroy an element that has trigger interactions with other objects you need to code the correct reactions into the code that decides to destroy the object.
Imagine you are standing in a house which is suddenly completely vaporized by an invading aliens main cannon, leaving you standing on a patch of perfectly flat open ground. Did you exit the house? No. There just isn’t a house any more. Same deal for vanishing colliders.
I eventually got it working by calling a method of the player gameobject from the mine gameobject.
Mine script:
if (distance < 10)
{
GameObject ship = GameObject.FindGameObjectWithTag(“Player”);
ShipController controller = (ShipController)ship.GetComponent(typeof(ShipController));
controller.DestroyMine();
Destroy(this.gameObject);
}
Player script:
public void DestroyMine()
{
OnTriggerExit (null);
health -= 25;
}
void OnTriggerExit(Collider other)
{
underAttack = false;
}