Health above head problem

I display the health above he head of the character with 3d text but it gives me a null reference when i change it wiht this.

GetComponent.<TextMesh>().text = "Health:  " + health;

The text mesh’s name is Health. And i tried replacing TextMesh with health but nothing happened. This script is in the update function.

Is this script on the object with the text mesh?

GetComponent<> gets a component of the given type from the current gameObject. If you want set the textmesh of a different gameobject, you would need to have a reference to it first.

The easiest way to solve this, is to create a public TextMesh variable and then drag the object into the inspector field that is created for that variable. Then in the script you just do “variablename.text = …”

Another way would be to find the correct gameobject first using one of the find functions, or by also creating a public variable for that. And then do GetComponent on that gameobject.

Its a child of the object. And how would i make a TextMesh Variable? Would it just be a gameobject?

var textmesh: TextMesh;

Thanks