Public Text Error

I put that line:

public Text healthText;

but when i start to play i have that error:

    NullReferenceException: Object reference not set to an instance of an object
    PlayerHealth.Update () (at Assets/Scripts/PlayerHealth.cs:51)

I fixed, i maked a other script:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class HealthCountUI : MonoBehaviour {


    Text healthText;

    public PlayerHealth Ph;


    void Awake()
    {
        healthText = GetComponent<Text>();
    }


    void Update()
    {
        healthText.text = "Health: " + Ph.currentHealth;
    }
}

the “NullReferenceException” would of been because you did not assign it in the inspector.

1 Like
1 Like