- Home /
OnTriggerExit not called on Destroy()
I instantiate a gameObject that immediately intersects with another gameObject. For both OnTriggerEnter is being fired. When I now destroy one of them, OnTriggerExit is not being fired on the one that is still there. Is this a bug or does this count as intended?
I need to get notice of the fact that the intersection no longer persists, and OnTriggerExit would have been handy here.
Is there another way to do this?
Answer by Ghopper21 · Feb 09, 2013 at 05:04 PM
This is almost certainly intended, as after you destroy the object there's no Collider
component left to pass to the other object's OnTriggerExit
function.
Here's a simple way to handle it, by putting your exit trigger logic in Update
where you can check whether the triggering object has been destroyed:
boolean triggered = false;
Collider other;
void OnTriggerEnter( Collider other )
{
triggered = true;
this.other = other;
// put other enter trigger logic here
}
void Update()
{
// check if we've been triggered but the other object has been destroyed
if ( triggered && !other )
{
// put exit trigger logic here
}
// put other update logic here
}
I saw the date, but i need to thank you for the reply. You method fix my problem.
Answer by RodrigoAbreu · Aug 05, 2018 at 06:53 PM
This bug seems to be back in Unity 2018.2.0f2, but I worked around it by implementing observers, so I notify the objects interested in the subject's status, including when it's destroyed.
This shit is in Unity 2018.3.5f, although maybe it should work?
Answer by pad406 · Feb 09, 2013 at 12:55 AM
Sorry, dont know if it's a bug or not. As an alternative you could use sendmessage or broadcastmessage
I guess it is not triggerd because the OnTriggerExit() would not have a parameter anymore since the other object does no longer exist. (OnTriggerExit hads over the collider of the object that caused the collision).
I´m thinking of moving the object very far away first and then call Destroy() on it. But I´m not sure if this will be reliable (ti$$anonymous$$g).
Should work do a delayed destroy of you're concerned about ti$$anonymous$$g
hm... OnTriggerStay()
, and check if the other's still there? OnTriggerExit()
won't be called because that's a kind of collision, just a "negative" one.. you can't compute collision with an object marked for deletion, there would be no point in calling Destroy()
then. You can use a Send$$anonymous$$essage although an event would be better.. Any object could subscribe to any other object to get notified of it's destroyment :)
Answer by amitDklein · Sep 19, 2021 at 09:56 AM
I just use
this.GetComponent<SphereCollider>().radius = 0f;
Destroy(this.gameObject);
Then when the radius it 0 the collider exit before you destroy it
I dont think that this work.
When you reduce radius/size to 0 it will trigger a OnTriggerExit event but also a OnTriggerEnter event subsequently.
So -1 +1 = 0, no effect.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Destroy all GameObjects EXCEPT some... 2 Answers
About Tutorial "Space Shooter" - Problem with shooting and boundary 0 Answers
Destroyed gameobject found with FindObjectsOfType<>? 1 Answer
Checking to see if there are no more of a certain object in the scene? 1 Answer