Question by
terror2012 · Jan 14, 2017 at 11:07 PM ·
c#gui
NullReferenceException on a public image, that was declared
public Image currentHealthbar;
public Text HealthText;
private void UpdateHealthBar()
{
float percentageHealth = Health / maxHealth;
currentHealthbar.rectTransform.localScale = new Vector3(percentageHealth, 1, 1);
HealthText.text = (percentageHealth * 100).ToString() + '%';
}
void Start()
{
maxHealth = 100;
Health = maxHealth;
UpdateHealthBar();
}
void Update()
{
if(Input.GetKey(KeyCode.LeftShift))
{
Health -= 1;
UpdateHealthBar();
Debug.Log(Health);
}
}
For some reasons, on Unity I'm getting: NullReferenceException: Object reference not set to an instance of an object PlayerHealth.UpdateHealthBar () (at Assets/Scripts/character/PlayerHealth.cs:44) PlayerHealth.Update () (at Assets/Scripts/character/PlayerHealth.cs:63) 1 time at the beginning of the game (void start) and each second I'm holding shift. I set the Image from the components (drag and drop) and text. When I press shift, instead of going slowly to 0, it drops to 0 instantly (both the text and the x scale). Thanks for help.
Comment