Search Unity

Hi, Very Basic Question About 2D Collision Detection

Discussion in 'Getting Started' started by Shark764, Jan 22, 2015.

  1. Shark764

    Shark764

    Joined:
    Jan 22, 2015
    Posts:
    2
    The problem is that even though I follow the YouTube tutorials on 2D collision, nothing happens. Here's my code:
    Code (csharp):
    1.  
    2.  
    3.         void onCollision2DEnter (Collision2D col)
    4.         {
    5.                 if  (col.gameObject.name  ==  "Plane") {
    6.                         Debug.Log ("worked"); // didn't log this to console
    7.                 }
    8.         }
    So, I made two quads & added a Rigidbody2D to one so that one would fall onto the other like a rock falling on the ground. Unfortunately, nothing gets logged to the console saying that the collision happened. Please, help!
     
    Last edited by a moderator: Jan 22, 2015
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The function name is incorrect. All code must be spelled and capitalized exactly correct.

    --Eric
     
  3. Shark764

    Shark764

    Joined:
    Jan 22, 2015
    Posts:
    2
    I fixed the function's spelling, but it still doesn't work. Is there anything else I can try? Perhaps another method of detecting collision?
     
    Last edited: Jan 23, 2015
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It works fine, if you made sure you have it correct (double check the docs) and attached it to an object that has a 2D collider.

    --Eric
     
  5. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    1. OnCollisionEnter2D
    2. Both should have collider2d