Why is OnTriggerExit not firing?
I want to detect if an enemy is inside a spotlight's range.
I have a Spotlight that has a cone mesh as a child. The cone is a trigger and I disable and enable it with the spacebar when the light is turned on or off.
I've set up the enemy code to be something like the following pseudocode:
OnTriggerEnter(other){
if(other == ConeMesh)
ReduceHitPoints();
}
OnTriggerExit(other){
if(other == ConeMesh)
IncreaseHitPoints();
}
The OnTriggerEnter part works great; however, the OnTriggerExit is not firing.
Let's say that the enemy is in the dark and you shine the light. This enables the cone's collider, it calls the enemy's OnTriggerEnter and the enemy's health is reduced. The enemy is now inside the light and I decide to turn it off. The light goes off, the cone's collider is disabled but OnTriggerExit is NOT called. Why?
Answer by Zoogyburger · May 02, 2016 at 09:51 PM
If your collider is disabled, how can it detect if it's in light or not?
Answer by RakNet · Apr 06, 2019 at 06:10 PM
Since this is a top result on Google I wanted to link to a more robust solution https://forum.unity.com/threads/fix-ontriggerexit-will-now-be-called-for-disabled-gameobjects-colliders.657205/
Answer by tanoshimi · May 02, 2016 at 09:51 PM
Because, from your description, the cone collider never exits the trigger... it is disabled while it is inside the trigger. A disabled collider, by definition, doesn't fire any OnTrigger events.
Your answer
Follow this Question
Related Questions
Trouble with collison and trigger's 1 Answer
ontriggerexit not working 0 Answers
How to detect collision of two moving characters? 1 Answer
Rotating Gameobject not Colliding 1 Answer