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

Noob needs help with C# HealthPickup

Discussion in 'Scripting' started by Christo_Katkos, Oct 17, 2014.

  1. Christo_Katkos

    Christo_Katkos

    Joined:
    Jan 7, 2014
    Posts:
    20
    Howdy!

    Im having trouble with my health pickup script.

    In my PlayerHealth Script:
    The player has a playerStartingHealth of 5.
    Then a playerCurrentHealth that keeps track of the player health when injured and at 0 dies.

    if the playerCurrentHealth != playerStaringHealth
    the player picks up the health, and it ++ to his health and the health object is turned off.

    up to this point it works but now...
    ----------------------------------------------------

    In my HealthPickup Script:
    I need to get the values from playerCurrentHealth and playerStartingHealth in my PlayerHealth Script into this, HealthPickup Script.

    I need to tell it if currentHealth != startingHealth
    the player can pick it up.

    When I run the game the player's health goes to 0 immediately and reloads the application level(0) over and over (he dies)

    Im guessing my problem is where Im trying to get the values from the PlayerHealth scirpt to my HeathPickup script.

    To recap
    I need to tell it if currentHealth != startingHealth
    the player can pick it up and the object turns off.


    Code (CSharp):
    1. public class HealthPickup : MonoBehaviour    
    2. {
    3.     private PlayerHealth playerhealth;
    4.     public int maxhealth;
    5.    
    6.     public int currentHealth;// ??
    7.     public int startingHealth;// ??
    8.    
    9.     void Awake()
    10.     {
    11.         playerhealth = GameObject.Find("Player").GetComponent<PlayerHealth>();// ??
    12.     }
    13.    
    14.     void Update()
    15.     {
    16.         playerhealth.playerCurrentHealth = currentHealth;// ??
    17.         playerhealth.playerStartingHealth = startingHealth;// ??
    18.     }
    19.  
    20.     void OnTriggerEnter(Collider other)
    21.     {
    22.         if (other.gameObject.tag == "Player"&& currentHealth != startingHealth)// ??
    23.         {
    24.             gameObject.SetActive(false);
    25.         }
    26.     }
    27. }
    28.  
     
  2. inihility

    inihility

    Joined:
    Oct 17, 2014
    Posts:
    7
    Instead of:

    Code (CSharp):
    1. if (other.gameObject.tag == "Player"&& currentHealth != startingHealth)
    Try:

    Code (CSharp):
    1. if (other.gameObject.tag == "Player"&& playerhealth.playerCurrentHealth != playerhealth.playerStartingHealth)
    It seems that what you are trying to do now is comparing two undeclared variables (currentHealth and startingHealth), so I would try ditching them entirely and making a direct comparison (playerhealth.playerCurrentHealth and playerhealth.playerStartingHealth).
     
    Christo_Katkos likes this.
  3. Christo_Katkos

    Christo_Katkos

    Joined:
    Jan 7, 2014
    Posts:
    20
    Game runs now but im getting this error:
    Object reference not set to an instance of an object
    HealthPickup.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/HealthPickup.cs:13)

    and the health pichup object doesnt dissapear, but get added.
     
  4. inihility

    inihility

    Joined:
    Oct 17, 2014
    Posts:
    7
    Are you trying to destroy the healthpickup? If so try Destroy(gameObject); instead of gameObject.SetActive("false");
     
  5. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Something I Noticed also that I imagine might cause some confusion later on.

    You told the script that if the players health does not equal starting health to pick up the item?
    If you end up putting other things in the game that can give the player higher amount of health than the starting health you're letting them waste power ups?

    Of course I could be misreading that so someone please correct me if I am wrong.
    Best Of Luck to you regardless.
     
  6. Christo_Katkos

    Christo_Katkos

    Joined:
    Jan 7, 2014
    Posts:
    20
    How it works is the

    player starts with 5/5 health(player starthealth)
    if currenthealth == 5/5(starthealth) he cant pickup anymore health.
    if currenthealth != 5/5(starthealth he can pickup health

    im planning to add something that increases the starthealth later.
     
  7. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Yeah I'm probably not reading it right which doesn't matter since you're the one writing it HAhaha.