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

Question Creation of a health pickup item.

Discussion in 'Scripting' started by dcgoodheart, Oct 10, 2020.

  1. dcgoodheart

    dcgoodheart

    Joined:
    Mar 17, 2019
    Posts:
    8
    Hello,
    I hate to say it but I'm very new to coding. I am trying to figure out how to create a gameobject that when it collides with the player will heal them a certain amount (1 in this case) and not go over the maximum amount.




    Code (CSharp):
    1. public class PlayerHealth : MonoBehaviour
    2. {
    3.     public int maxheart = 5;
    4.     public int playerHP = 5;
    5.     public Image[] hearts;
    6.  
    7.  
    8.     public Sprite fullHeart;
    9.     public Sprite emptyHeart;
    10.  
    11.  
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.        
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         uicontrol();
    23.         if (playerHP > maxheart)
    24.         {
    25.             playerHP = maxheart;
    26.         }
    27.             if (playerHP <= 0)
    28.             {
    29.                 Debug.Log(gameObject + "has died");
    30.             //should load a gameover scene
    31.            SceneManager.LoadScene("Gameover");
    32.             }
    33.        
    34.        
    35.     }
    36.  
    37.     public void playerDamage(int playerDMG)
    38.     {
    39.         playerHP = playerHP + playerDMG;
    40.     }
    41.  
    42.     public void uicontrol()
    43.     {
    44.        
    45.         if (playerHP > maxheart)
    46.         {
    47.             playerHP = maxheart;
    48.         }
    49.      
    50.         for(int i = 0; i < hearts.Length; i++)
    51.         {
    52.             if (i < playerHP)
    53.             {
    54.                 hearts[i].sprite = fullHeart;
    55.             }
    56.             else
    57.             {
    58.                 hearts[i].sprite = emptyHeart;
    59.             }
    60.             if(i < maxheart)
    61.             {
    62.                 hearts[i].enabled = true;
    63.             }
    64.             else
    65.             {
    66.                 hearts[i].enabled = false;
    67.             }
    68.  
    69.         }
    70.  
    71.        
    72.                
    73.     }
    74. }
    75.  




    This is the start of the pickup item code





    public class HealthPickup : MonoBehaviour
    {
    PlayerHealth playerHeartP; //Reference to the player's health when updated

    public float hpBonus = 1f; //how much hp recovered

    void Awake()
    {
    playerHeartP = FindObjectOfType<PlayerHealth>();
    }

    private void OnTriggerEnter(Collider other)
    {
    if(playerHeartP.playerHP < playerHeartP.maxheart)
    {
    Destroy(gameObject);
    playerHeartP.playerHP = playerHeartP.maxheart;
    }
    }
    }




    So the gameobject is destroyed but has no effect on the health. If anyone could lead me in the right direction, I would greatly apprecate it!
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Swap these two lines around in HealthPickup:
    Code (CSharp):
    1. Destroy(gameObject);
    2. playerHeartP.playerHP = playerHeartP.maxheart;
    You're destroying the script before it has a chance of updating the player's health.
     
  3. dcgoodheart

    dcgoodheart

    Joined:
    Mar 17, 2019
    Posts:
    8

    Ah that helped it recognize the next line, however how would I go about recovering just 1 versus the entire health?
     
  4. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    Code (CSharp):
    1. if(playerHeartP.playerHP < playerHeartP.maxheart {
    2. playerHeartP.playerHP += 1;
    3. }
    4. Destroy(gameObject);