Search Unity

Nevermind (User error) Understanding Collider 2d Behavior (Trigger Collider only works once)

Discussion in '2D' started by robert-sherwood, Jan 23, 2018.

  1. robert-sherwood

    robert-sherwood

    Joined:
    Jan 7, 2018
    Posts:
    14
    I am struggling up the Collider2d learning curve, and devised a minimum test case which illustrates my confusion. Consider the following code:

    Code (CSharp):
    1.     private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         if (collision.tag == "NPC")
    4.         {
    5.             Debug.Log("entering collider");
    6.         }
    7.     }
    8.  
    9.     private void OnTriggerExit2D(Collider2D collision)
    10.     {
    11.         if (collision.tag == "NPC")
    12.         {
    13.             Debug.Log("exiting collider");
    14.         }
    15.     }
    When I attach this to a player object, and go bump into an NPC, I would expect one of two outcomes:

    1. I messed something up and nothing gets logged.
    2. I got everything right and I get a log entry every time I enter or exit the Collider2D I've set up around the NPC.

    Instead, I get a log entry on the first entrance, and the first exit, and never afterwards.

    This suggests I'm missing something fundamental about how colliders work. Does anyone have any thoughts on what is going on?

    Thanks!
     
  2. robert-sherwood

    robert-sherwood

    Joined:
    Jan 7, 2018
    Posts:
    14
    Haha, nevermind - for other who are confused, the logging console will just add a number next to the first log entry to indicate if the log is repeated. This is not how I am used to logging working.