Search Unity

Health System

Discussion in 'Scripting' started by Ant10, Mar 21, 2019.

  1. Ant10

    Ant10

    Joined:
    Jul 1, 2017
    Posts:
    4
    Hello,
    I have a problem on the Life / damage button, now I will explain the anomaly that is hanging over me:

    I did something like this:



    To make the life and damage buttons work I made this C # script

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class HealthBarSlider : MonoBehaviour
    7. {
    8.  
    9.     public Slider slider;
    10.     public Button buttonVita;
    11.       public Button buttonDanno;
    12.  
    13.  
    14.      //public HealthSystem healthSystem = new HealthSystem();
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.    
    19.     {
    20.          
    21.         Button btnVita = buttonVita.GetComponent<Button>();
    22.         Button btnDanno = buttonDanno.GetComponent<Button>();
    23.  
    24.         btnVita.onClick.AddListener(ClickVita);
    25.         btnDanno.onClick.AddListener(ClickDanno);
    26.  
    27. }
    28.  
    29. void ClickVita(){
    30.     health(10);
    31.      Debug.Log ("Vita: " + GetHealth());
    32. }
    33.     void ClickDanno(){
    34.  
    35.     damage(10);
    36.      Debug.Log ("Danno: " + GetHealth());
    37. }
    38.  
    39. public void health(int healthAmount){
    40.  
    41. slider.value+=healthAmount;
    42.  
    43.     }
    44.  
    45.       public void damage(int healthAmount){
    46.  
    47. slider.value-=healthAmount;
    48.  
    49.     }
    50.  
    51.       public float GetHealth(){
    52.       return (float) slider.value;
    53.   }
    54. void Update()
    55.     {
    56.        
    57.     }
    58.  
    59.     }
    60.  
    And so far everything is ok. If I press the button, it gives Life and decreases and increases the slider that is at the bottom left.

    Then I tried to divide the code into 2 scripts like this:


    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class HealthSystem
    7. {
    8.    
    9. private Slider slider;
    10.  
    11.     public void health(int healthAmount){
    12.  
    13. slider.value+=healthAmount;
    14.  
    15.     }
    16.  
    17.       public void damage(int healthAmount){
    18.  
    19. slider.value-=healthAmount;
    20.  
    21.     }
    22.  
    23.       public float GetHealth(){
    24.       return (float) slider.value;
    25.   }
    26. }
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class HealthBarSlider : MonoBehaviour
    7. {
    8.  
    9.    public HealthSystem healthSystem;
    10.  
    11.     public Button buttonVita;
    12.       public Button buttonDanno;
    13.  
    14.  
    15.      //public HealthSystem healthSystem = new HealthSystem();
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.    
    20.     {
    21.          
    22.         Button btnVita = buttonVita.GetComponent<Button>();
    23.         Button btnDanno = buttonDanno.GetComponent<Button>();
    24.  
    25.         btnVita.onClick.AddListener(ClickVita);
    26.         btnDanno.onClick.AddListener(ClickDanno);
    27.  
    28. }
    29.  
    30. void ClickVita(){
    31.     healthSystem.health(10);
    32.      Debug.Log ("Vita: " + healthSystem.GetHealth());
    33. }
    34.     void ClickDanno(){
    35.  
    36.     healthSystem.damage(10);
    37.      Debug.Log ("Danno: " + healthSystem.GetHealth());
    38. }
    39.  
    40.  
    41. void Update()
    42.     {
    43.        
    44.     }
    45.  
    46.     }
    But when I try to use the life or damage button I get this error:



    How can I handle this problem?

    Thank you :)
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    A NullReferenceException generally means that you aren't properly initializing your variables. I suggest you double-check the values of "healthSystem" in your HealthBarSlider script and "slider" in your HealthSystem script.
     
    SparrowGS likes this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Assign the healthSystem component to the script.
     
  4. Ant10

    Ant10

    Joined:
    Jul 1, 2017
    Posts:
    4
    Hi, could you tell me how I could do it? thank you