- Home /
How to manually call/fire OnTriggerExit() for colliders a trigger is inside?
I'm trying to detect when objects have left a collider when they are destroyed/disabled, so that I can stop attacking that object, as well as removing it from a collection it is stored in.
The documentation says "OnTriggerExit is called when the Collider other has stopped touching the trigger."
, understandably confusing when thinking about a disabled object. I see threads ranging back 4 years asking about this issue. It seems the acceptable workaround is to move your object up a few thousand world units, this probably works, but it seems like a hacky solution to something that should be more elegant.
Can I somehow call OnTriggerExit from the collider(s) the object is currently in when it is disabled? Or a way to get OnTriggerExit to be called automatically when an object is disabled?
Answer by sotirosn · Feb 07, 2016 at 01:44 AM
I think it is ridiculous that the OnTriggerExit is not called when the component is disabled. It should be called right before the object is disabled so that it can be properly handled by other objects that need to know what the heck they are touching. So basically it should work like this.
gameObject.SetActive(false); -> OnTriggerExit() { ... } -> OnDisable() { ... } -> object is now disabled
I don't understand why this is so hard. The best solution really is to move the freaken object out to the edge of space and then disable it.
Answer by sumeeton · Mar 30, 2015 at 05:00 AM
When you destroy the object or disable the object, the components on the object won't work which includes the script components too. So you'll have to make sure that the object stays alive for the collision trigger.
One way is to disable the renderer, so the scripts and colliders are still working but the object won't be displayed. You can destroy the object when you no longer need it. Like OnTriggerExit.
Another way is to instantiate another empty object with the same colliders and script components at the place of the object you destroyed.
The object that is to be destroyed/disabled (object pooling) is no longer needed at the time it is to be destroyed/disabled. However the objects owning the colliders keep record of what come sin and goes out, and they are unable to record the object leaving if it is destroyed while within the bounds of the collider.
Yes, that was my answer.
Use two variables: bool insideCollider = false; bool destroySelf = false;
OnTriggerEnter (Collider col)
{
insideCollider = true;
}
OnTriggerExit (Collider col)
{
// do your stuff here
if (destroySelf)
{
// Destroy the gameobject here
}
insideCollider = false;
}
void DestroyGameObject ()
{
if (insideCollider)
{
// disable the renderer
destroySelf = true;
}
else
{
// destroy the gameobject here
}
}
Ins$$anonymous$$d of destroying the object, call the function DestroyGameObject(). This way if it's inside the collider, it will only disable the renderer and when game object leaves the collider it will destory itself.
This might cause the OnTriggerExit to be not called on the other object since the gameobject is destroyed. So make the destroySelf property public and do not destroy the object in OnTriggerExit in this script. But check the destroySelf property in the colliders and then destroy the object.
Your answer
Follow this Question
Related Questions
Internal collisions 1 Answer
Are there any way to show an objects collider outline through scripting? 3 Answers
Get collision without attaching a script to every game object 1 Answer
Need some help an object collider that stops a timer then displays it on another scene 1 Answer
Determine which collider was clicked 1 Answer