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): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class HealthBarSlider : MonoBehaviour { public Slider slider; public Button buttonVita; public Button buttonDanno; //public HealthSystem healthSystem = new HealthSystem(); // Start is called before the first frame update void Start() { Button btnVita = buttonVita.GetComponent<Button>(); Button btnDanno = buttonDanno.GetComponent<Button>(); btnVita.onClick.AddListener(ClickVita); btnDanno.onClick.AddListener(ClickDanno); } void ClickVita(){ health(10); Debug.Log ("Vita: " + GetHealth()); } void ClickDanno(){ damage(10); Debug.Log ("Danno: " + GetHealth()); } public void health(int healthAmount){ slider.value+=healthAmount; } public void damage(int healthAmount){ slider.value-=healthAmount; } public float GetHealth(){ return (float) slider.value; } void Update() { } } 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): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class HealthSystem { private Slider slider; public void health(int healthAmount){ slider.value+=healthAmount; } public void damage(int healthAmount){ slider.value-=healthAmount; } public float GetHealth(){ return (float) slider.value; } } Code (csharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class HealthBarSlider : MonoBehaviour { public HealthSystem healthSystem; public Button buttonVita; public Button buttonDanno; //public HealthSystem healthSystem = new HealthSystem(); // Start is called before the first frame update void Start() { Button btnVita = buttonVita.GetComponent<Button>(); Button btnDanno = buttonDanno.GetComponent<Button>(); btnVita.onClick.AddListener(ClickVita); btnDanno.onClick.AddListener(ClickDanno); } void ClickVita(){ healthSystem.health(10); Debug.Log ("Vita: " + healthSystem.GetHealth()); } void ClickDanno(){ healthSystem.damage(10); Debug.Log ("Danno: " + healthSystem.GetHealth()); } void Update() { } } But when I try to use the life or damage button I get this error: How can I handle this problem? Thank you
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.