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

2D Platformer Coin Collection system Problems.

Discussion in '2D' started by Obamanation, Sep 11, 2014.

  1. Obamanation

    Obamanation

    Joined:
    Sep 11, 2014
    Posts:
    2
    Hi guys,
    With my 2D Platformer I have a script which makes it where you run into the coin and the coin destroys and you will have 1 point added to your score however the destroy system doesn't work. The gameobject doesn't destroy once my character runs into it. Any help would be great thanks.

    Code (JavaScript):
    1. var score = 0;
    2.  
    3. function OnTriggerEnter( other : Collider ) {  
    4.    if (other.tag == "Coin") {      
    5.       score += 5;      
    6.       Destroy(other.gameObject);
    7.    }
    8. }
     
  2. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
  3. Obamanation

    Obamanation

    Joined:
    Sep 11, 2014
    Posts:
    2
    I tried changing t to a OnTriggerEnter2D and it still doesn't work. Where exactly in the code should i put the debug log?
     
  4. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    Code (CSharp):
    1. function OnTriggerEnter( other : Collider ) {
    2.    Debug.Log(other.tag);
    3.    if (other.tag == "Coin") {    
    4.       score += 5;    
    5.       Destroy(other.gameObject);
    6.    }
    7. }
    You just use OnTriggerEnter2D if you are using RigidBody2D.