Search Unity

Adding Health to a Player

Discussion in '2D' started by iningomontoya, Aug 14, 2017.

  1. iningomontoya

    iningomontoya

    Joined:
    Jul 27, 2017
    Posts:
    21
    I have a feature in my game where a laser is firing at the player. I want the player to also be able to collect objects that would increase the players health. Here is the code that I used to try to increase the health:
    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D other)
    2.          {
    3.            if(other.gameObject.name == "Coin")
    4.            {
    5.               _healthPoints += 10000;
    6.          }
    7.      }
    I have also tried tags but that has not worked either.
     
  2. iningomontoya

    iningomontoya

    Joined:
    Jul 27, 2017
    Posts:
    21
    I am happy to report back that I got it to work. I realized that the tag code was slightly different so I just modified the code above replacing name with tag and it worked.

    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D other)
    2.          {
    3.            if(other.gameObject.tag == "Coin")
    4.            {
    5.               _healthPoints += 10000;
    6.          }
    7.      }
     
  3. Deleted User

    Deleted User

    Guest

    If you're using name make sure the name is the same as the name of the gameobject with the collier on it, it won't detect a parent or child object's name
     
  4. iningomontoya

    iningomontoya

    Joined:
    Jul 27, 2017
    Posts:
    21
    Okay thanks. I am fairly certain the name was correct but I will check again on that.