Search Unity

Help With OnCollision2D {Resolved}

Discussion in 'Scripting' started by Blar321, Sep 2, 2015.

  1. Blar321

    Blar321

    Joined:
    Aug 30, 2015
    Posts:
    25
    Hello everybody I was working on a recent project when I came across this problem. When I run it and I make this GameObject, "Player", collide with "Coin" the console only outputs "Working 1." Any suggestions help! Thank you so much!
    Code (CSharp):
    1.     void OnCollisionEnter2D(Collision2D other){
    2.         Debug.Log ("Working 1");
    3.         if(other.gameObject.tag == "Coin"){
    4.             Debug.Log ("Working 2");
    5.             Destroy(other.gameObject);
    6.            
    7.         }
    8.     }
    9.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Is it tagged as Coin or is it named Coin?
     
  3. Blar321

    Blar321

    Joined:
    Aug 30, 2015
    Posts:
    25
    It is named. Can you explain the difference?
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    The manual on tags does a much better job of explaining it than I could here in this post.

    For now, you could just change your code to use the name instead of the tag:
    Code (csharp):
    1.  
    2. if(other.gameObject.name == "Coin"){
    3.  
    But that might fall apart in the future if you have different coin prefabs (say blue and red) or instantiate them via code (then they'll be called "Coin (Clone)"), so I'd still recommend you learn about tags and assign them a coin tag.
     
  5. Blar321

    Blar321

    Joined:
    Aug 30, 2015
    Posts:
    25
    Thank you so much!