Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

OnTriggerEnter2D firing twice

Discussion in '2D' started by cdutoit, Feb 17, 2015.

  1. cdutoit

    cdutoit

    Joined:
    Jul 20, 2013
    Posts:
    34
    I'm stumped.

    Remember the good old frogger game? Well, I'm recreating that. Except I've run into an issue where the OnTriggerEnter2D event is firing twice.

    Here is the method on the "truck" object:

    Code (csharp):
    1.  
    2. void OnTriggerEnter2D(Collider2Dcoll) {
    3.    // Did Truck hit the Frog?
    4.  
    5.    if (coll.name == "Frog") {
    6.  
    7.      //GameOver
    8.      gameObject.SetActive(false);
    9.      Destroy(coll.gameObject);
    10.      skeeper.loseLife();
    11.     Debug.Log ("OUCH");
    12.   }
    13. }
    14.  
    Truck is a Rigibody (Kinematic checked or uncheck makes no difference) with a collider attached, marked as Is Trigger.

    Frog is a Rigibody with a collider attached.

    Now when the Truck comes to squash my Frog, this OnTriggerEnter2D fires. Problem is it fires twice. The Debug "OUCH" statement appears twice in my log, and 2 lives are decremented thanks to skeeper.loseLife() being called twice.

    I really thought this would only fire once. I've seen a bug report about this existing in Unity 4.5 but marked as fixed.

    Any suggestions as to why this event would fire twice?

    Thanks
    Chris
    EDIT: Fixed Typo in description of method.
     
    Last edited: Feb 17, 2015
  2. JayJennings

    JayJennings

    Joined:
    Jun 24, 2013
    Posts:
    184
    Your code shows OnTriggerEnter2D -- make sure you don't also have a dupe function called OnTriggerStay2D (you know, like messing around trying things before settling on a final solution).

    Jay
     
  3. cdutoit

    cdutoit

    Joined:
    Jul 20, 2013
    Posts:
    34
    Hi Jay - thanks for the suggestion. Unfortunately that's not it. I did have a typo in my description calling it Stay when I meant trigger (thanks for pointing that out) - The code is correct - it is OnTriggerEnter2D firing twice.

    The only method I have on the Truck is onTriggerEnter. And it's the only place throughout the entire project where "OUCH" is output to the log and the loseLife() method is called.

    Since it's firing twice, you lose two lives everytime the truck hits you. Very perplexing. I'm on Unity4.6.4f1 - I'm really starting to think this is a bizarre bug.
     
  4. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    Whenever I have this issue (last time was last night), it's because more than one collision actually happens. What's the shape of the different colliding objects? They are probably touching in two places.

    If you can't find a simple way to work around it, set a flag in the collider and check for that before doing the life-taking action. Reset it after moving the frog back.
     
    theANMATOR2b likes this.
  5. cdutoit

    cdutoit

    Joined:
    Jul 20, 2013
    Posts:
    34
    Ah thanks everybody for the pointers etc. Turns out it was my mistake - Don't you hate it when Unity does what you tell it ;)

    I inadvertently had the script with the OnTrigger attached to my object twice. Quite interesting...I had no idea you could do that - so that caused two ontriggers to be fired.

    Live and learn!

    Thanks again.
     
    namlunoy likes this.
  6. namlunoy

    namlunoy

    Joined:
    Apr 15, 2014
    Posts:
    32
    oh, i learn something! :)