Search Unity

OnTriggerEnter2D isn't called consistently

Discussion in '2D' started by MatLee, Feb 3, 2015.

  1. MatLee

    MatLee

    Joined:
    Feb 2, 2015
    Posts:
    1
    Hey guys,

    I need your help! I'm two days with this problem, I don't know what to do. =/

    I have two objects: Player and Enemy (Spawn). When I press play for the first time, the Animation works very well at the collision.. But when a press play for the second time, sometimes works, sometimes not.

    Please see it:

    (Player)
    void OnTriggerEnter2D(Collider2D collider)
    {
    Debug.Log("Before if");
    if (collider.gameObject.tag == "Enemy")
    {
    Debug.Log("After if");
    _animator.SetBool("hit", true);

    StartCoroutine(WaitSeconds());
    }
    }

    IEnumerator WaitSeconds()
    {
    Debug.Log("IEnumerator");
    yield return new WaitForSeconds(0.2f);

    _animator.SetBool("hit", false);
    }

    As I was saying, when it dosn't work, OnTriggerEnter2D isn't called. I had a suspicion, but I don't think is it. I have OnTriggerEnter2D in Player and Enemy's Class, but both do different tasks. And I wanted to keep a low coupling, Therefore, isolated classes.

    In the both classes, I checked "Is Trigger" at the Circle Collider 2D, and in Player I put the Rigidbody 2D.

    If necessary, I can show some prints, or the code or a video! =]

    Please... Help me! =(