Question by
overki11 · Jan 11, 2016 at 03:44 PM ·
nullreferenceexceptionnullhudhealth
NullReferenceException when changing Canvas text via code
Hi. I've been trying to add a health counter to my game, by changing canvas text via code. I get a NullReferenceException whenever I use it though. This is the code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UIScript : MonoBehaviour {
public Text HealthText;
int PrevHealth;
// Use this for initialization
void Start () {
HealthText.text = "Health: " + GameObject.Find("FPSController").GetComponent<PlyHealthScript>().curHealth;
HealthText = GameObject.Find("FPSController").GetComponent<Text>();
PrevHealth = GameObject.Find("FPSController").GetComponent<PlyHealthScript>().curHealth;
}
// Update is called once per frame
void Update () {
}
}
Here is the error:
NullReferenceException: Object reference not set to an instance of an object
UIScript.Start () (at Assets/Scripts/UIScript.cs:11)
Can anyone help? It's been driving me crazy.
Comment