- Home /
Unity 4.6 UI Slider NullReferenceException
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...
A simpler way would be to use a UI Image component and change the image Type by "Filled" set Fill $$anonymous$$ethod vertical or horizontal and the origin depending on what you need (you don't really need the whole slider system). Fill amount will allow you to animate your health bar.
I'm aware of that, but I went through the official Unity tutorials and I'm pretty sure they mentioned that the slider could be used for this purpose too..
If I'm unable to resolve this I will probably switch to the way you proposed, but it would be nice to know what exactly is causing the exception and keep that in $$anonymous$$d for future reference...
Ok well then, give us the full error log and line if there is one.
$$anonymous$$aybe the slider is trying to access the hanlder you deleted.
The log file is huge..
The only reference to the Healthscript is this
NullReferenceException: Object reference not set to an instance of an object
at HealthScript.Update () [0x00000] in H:\_Unity Projects_\project\Assets\Scripts\HealthScript.cs:25
, which keeps repeating (expected, since it's inside the Update function)
Sorry, I accidentally replied as a new answer ins$$anonymous$$d of just adding a new comment...
I think the new GUI objects are not exactly the same as normal GameObjects. For one, they have a different transform class... Because of this, I suspect that maybe your script isn't recognizing its owner as a standard Game object...
Try embedding the slider control in an ordinary parent Game object ins$$anonymous$$d. Then attach your script to the Parent object in s$$anonymous$$d. Then use a find method to locate and assign the Slider object... e.g. "GameObject temp = GameObject.Find("Volume Slider");" or "GameObject temp = GameObject.transform.Find("Volume Slider").gameObject;" or something like that...
Answer by DiegoSLTS · Apr 09, 2015 at 01:28 PM
The line where you get the Slider component is probably returning null, make sure the game object with the HealthScript component attached also has the Slider component attached.
I figured out what was causing the problem. I did have the script attached to a gameobject with a slider, but I had accidentally attached it to a second empty GameObject.
Your answer
Follow this Question
Related Questions
Can't drag slider onto inspector 1 Answer
Lagging/unmoving slider 0 Answers
How to Make a Vertical-Scroll Number Input Field, i.e. < > Value Amount 0 Answers
Slider with max value in the middle 0 Answers