1

I have script for Hart counting, when enemy hit player, -1 life etc.

Everything work perfect, Value show on the screen, decreasing like it supposed to.

But in CONSOLE I'm getting this error:

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

can this stay like that? as everything works etc. but it's kind of annoying to have this error.

public class HartCount : MonoBehaviour
{
    public static int HartValue = 10;
    Text Hart;

    // Use this for initialization
    void Start()
    {
        Hart = GetComponent<Text>();
    }

    // Update is called once per frame
    void Update()
    {
        Hart.text = "" + HartValue;        
    }
}
Rufus L
  • 32,853
  • 5
  • 25
  • 38
  • Which line is 31? You only included 17 lines. – Draco18s no longer trusts SE Aug 15 '19 at 22:55
  • 2
    You have added the `HartCount` script to an object that doesn't have a `Text` component attached. You can search for `HartCount` in search field in the GameObject hierarchy view to find all GameObjects with this script attached, to find which object it is. – Fredrik Schön Aug 15 '19 at 23:17
  • See: [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/q/4660142/3744182). – dbc Aug 16 '19 at 00:21

1 Answers1

1

The HartCount script is attached to a GameObject that doesn't exist. Double check this, set a breakpoint after Hart = GetComponent<Text>(); and make sure that value is populated with the component. `

magna_nz
  • 1,087
  • 4
  • 17
  • 36