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

When player collecting coins then death

Discussion in 'Scripting' started by sayg0n, Dec 10, 2014.

  1. sayg0n

    sayg0n

    Joined:
    Dec 10, 2014
    Posts:
    2
    Hi,
    i have one problem with my c# script and i don't have any idea how can i resolved it.
    In game is object named "coin" (tag:"Coins") when player enter on trigger it then causes death.
    Below is this script.
    I hope there is some people who can help me ;) And of course sorry for my weak English...

    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D collider)
    2.     {
    3.         if (collider.gameObject.CompareTag("Coins"))
    4.             CollectCoin(collider);
    5.         else
    6.             HitBykwadrat(collider);
    7.     }
    8.        
    9.     void HitBykwadrat(Collider2D laserCollider)
    10.     {
    11.         if (!dead)
    12.             laserCollider.gameObject.audio.Play();
    13.  
    14.         dead = true;
    15.  
    16.         animator.SetBool("dead", true);
    17.     }
    18.  
    19.     void CollectCoin(Collider2D coinCollider)
    20.     {
    21.         coins++;
    22.        
    23.         Destroy(coinCollider.gameObject);
    24.  
    25.         AudioSource.PlayClipAtPoint(dźwiękZebraniaMonety, transform.position);
    26.     }
     

    Attached Files:

  2. puppeteer

    puppeteer

    Joined:
    Sep 15, 2010
    Posts:
    1,282
    Your code works just fine for me. I pick up any object that is tagged "Coins".

    So, I'm going to guess that your tag may be incorrectly named. It could have been written with an extra space like this: "Coins ", or " Coins"

    Try to delete your tag from the tags list and make it again.
     
  3. Kinos141

    Kinos141

    Joined:
    Jun 22, 2011
    Posts:
    969
    Code (csharp):
    1.  
    2. [LIST=1]
    3. [*]void OnTriggerEnter2D(Collider2D collider)
    4. [*]   {
    5. [*]       if (collider.gameObject.CompareTag("Coins"))
    6. [*]            CollectCoin(collider);
    7. [*]       else
    8. [*]            HitBykwadrat(collider);
    9. [*]   }
    10. [/LIST]
    11.  
    Why do you have it setup to if you collect coin or you die?
     
  4. sayg0n

    sayg0n

    Joined:
    Dec 10, 2014
    Posts:
    2
    Ok, i solved it.
    So, problem was that, my first tag name for coins was just "coin" i do some prefabs with objects that have tag "coin" and i forget change tag to "Coins". Now when changed tag on all prefabs objects, player may collect coins.

    Thanks @puppeteer for your helpful answer.
    @Kinos141 if you have proposal for better code, I'm glad to hear it ;)
     
    Last edited: Dec 10, 2014