- Home /
TriggerOnExit not activating after Destroy
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?
Does the $$anonymous$$e have a rigidbody attached? If not I suggest that you add one with is$$anonymous$$inematic set to true. The collider should then immediately be gone. In general you should not have colliders without rigidbodies for anything that might move or vanish.
Answer by MarkFinn · Dec 03, 2012 at 05:10 AM
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.
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.
I get that, but that's very vague. If I knew what those were I wouldn't be asking.
Ah, I mean that you need to perform the same actions as are in your TriggerOnExit in the code that calls Destroy on the $$anonymous$$e (assu$$anonymous$$g that you want the same thing to occur).
Best done by moving the contents of TriggerOnExit to a new function/method and calling that from TriggerOnExit and from the code that calls Destroy.
Answer by Zeek Aran · Dec 05, 2012 at 10:14 PM
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; }
Your answer
Follow this Question
Related Questions
Why is this rigidbody not activating the trigger? 0 Answers
Putting an object somewhere to destroy something else 1 Answer
destroying instructions sequence 1 Answer
gui does not appear after trigger? 1 Answer
OnTriggerEnter not working 1 Answer