- Home /
OnTriggerExit2D not working, but OnTriggerEnter2D does???
Just as the title states. I have an enemy and when he collides with a boxCollider2D the OnTriggerEnter2D works, but I cant even get a Debug.Log("test"); to show up on an OnTriggerExit2D.
What gives???
Answer by benatfantasylabs · Sep 01, 2017 at 05:40 PM
Check your spelling and capitalisation
If that doesnt help you might need to paste some code for anyone to be able to help
Answer by Geads · Sep 01, 2017 at 05:43 PM
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Edge")
{
enemy.ChangeDirection();
}
if (other.tag == "JumpTrigger")
{
enemy.EnemyJump();
Debug.Log("Jump Test - On Enter");
}
if (other.tag == "LandTrigger")
{
enemy.EnemyLand();
Debug.Log("Land test - On Enter");
}
}
public void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "JumpTrigger")
{
Debug.Log("Jump Test - On Exit");
}
}
On Enter works, On Exit does not.
?????
What does enemy.EnemyJump(); do?
Without seeing any code i would guess that it is because you are moving the collider out in the same frame (I dont think you can have an enter and an exit happen in the same frame so the exit gets missed)
You could try making the jump function set a member variable and in update in the next frame react to that
EnemyJump(); just sets jump = true; so the enemy jumps.
So the enemy hits a collider, jump = true; when the enemy is jumping they leave the collider so the OnTriggerExit2D should run, but it doesn't
Answer by PWL_Para · Sep 01, 2017 at 05:47 PM
It is soo hard to answer with such little information... :(
There are soo many things to check. 1:Check which colliders are really used. 2:See if any colliders are getting Disabled/Becoming Non Trigger/Sizes/Changing Layers/ etc.. 3:Check if one of at least one rigid body stays active all along. 4:Check if script is enabled all along. etc...
Sooo soo many things could have gone wrong.
If all the OnTriggerEnter2D's are working then why does it matter what is inside them? The script is enabled because the OnTriggerEnter2D's are working. Its the same collider, I don't understand why It works on the onEnter and not onExit. It's all the same colliders.
Answer by Raimi · Sep 01, 2017 at 09:25 PM
Is your enemy actually leaving the the trigger area? if not, on trigger exit wont be called.
e.g. Enemy enters area, enemy is destroyed. Trigger exit wont be called.
e.g Enemy enters area, Trigger is destroy. Trigger exit wont be called.
e.g Enemy enters area, Passes through and out of area. Trigger exit will be called.
Answer by guilhermecorintho · Sep 05, 2019 at 02:04 AM
Try to change the place of " Debug.Log("Jump Test - On Exit");" to outside of your if condition [if (other.tag == "JumpTrigger")] to see if the problem is really if the OnTriggerExit2D call or it is up to your IF statement
Your answer
![](https://koobas.hobune.stream/wayback/20220612134015im_/https://answers.unity.com/themes/thub/images/avi.jpg)