I’m trying to migrate from NGUI to the new native UI system, I’m getting a strange NullReferenceException in my slider…
First of all, I use my slider as a healthbar, so I’ve deleted the handle object and disabled “interactable”.
I’ve attached a script to the slider object, this script
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class HealthScript : MonoBehaviour {
public static bool eatNow;
// public GameObject pHealthBar;
Slider healthSlider;
float health;
// float healthValue;
void Start(){
health = 100f;
eatNow = false;
healthSlider = GetComponent<Slider>();
// healthValue = healthSlider.value;
}
void Update(){
// health = health - 0.05f;
healthSlider.value = health;
health = health - 0.05f;
Debug.Log (health);
if (eatNow == true){
eatNow = false;
HealthRise();
}
}
I get an exception in a line in the update
healthSlider.value = health;
I’ve tried declaring healthSlider as public, but it still throws the exception…
What’s wrong with my code? I’ve managed to resolve nullreferenceexceptions errors in the past, I’m stuck with this one…