Daniel Blanco
Daniel Blanco

Reputation: 97

OnTriggerEnter and OnTriggerExit called twice despite checking it

I have read a lot of questions about this problem but i couldn't solve it yet. I have one ball with a Sphere Collider and an invisible wall to restart the ball when it passes through it (on the onTriggerExit method). The problem is that i've not been able to solve it, even with a boolean to avoid entering the method.

public class ballRestart : MonoBehaviour
{
    shootController instance;
    bool isColliding;
    // Use this for initialization
    void Start()
    {
        instance = Camera.main.GetComponent<shootController>();
        isColliding = false;
    }


    public void OnTriggerEnter(Collider col)
    {
        Debug.Log("TeEnter: " + isColliding);
        if (!isColliding)
        {
            isColliding = true;
            Debug.Log("TRIGGERED: " + isColliding);
        }

    }

    void OnTriggerExit(Collider hit)
    {
        Debug.Log("TeExit: " + isColliding);
        if (isColliding)
        {
            instance.initializeBall();
            isColliding = false;
            Debug.Log("exit");
        }
    }
}

OUTPUT:

Here is the output of the Logs

As you see, it enters twice each time the ball enters the collider and twice each time the ball exits the same collider. I don't know what happens here.

Upvotes: 3

Views: 2306

Answers (1)

Dzianis Yafimau
Dzianis Yafimau

Reputation: 2016

I'm pretty sure you have 2 colliders on your object, each of them causes event triggering. Check your components and child objects.

P.s.: I know it is old question, but my answer can be helpful for others.

Upvotes: 2

Related Questions