My script won't let me change the text of a UI object

For some reason, this code is giving me the error “ArgumentException: Type cannot be null.” I couldn’t find any answers as to why. Can anyone help? UI(); is in the Update function, by the way.

var HPLabel:GameObject;

function UI()
{
    HPLabel.GetComponent(UI.Text).text = HP + "/" + maxHP;
}

I’m not sure, sinse I basically only code in C#, but sinse it claims the argument is null, does changing “UI.Text” to “UnityEngine.UI.Text” work? sinse this is the only method argument I see in this piece of code :stuck_out_tongue: If not, check whether both HP and maxHP are not null.

According to the docs, you get that error when:

The exception that is thrown when one of the arguments provided to a method is not valid.

hello @rct3fan24

 HPLabel.GetComponent(UI.Text).text = HP.ToString() + "/" + maxHP.ToString();

If your Function is on update then your should drag your text in inspector or you can Get it at start function once.

And make sure HP and maxHP is not null. try to debug

Are you sure there is a component attached to the HPLabel? OR maybe you didn’t assign the HPLabel gameObject?

Also a good tip: Drop JavaScript, go to C#, more than 90% of the plugins are C#, and the syntax isn’t that different.

Not a Unityscript user, but it seems to me to bad practice to have a function called “UI”, when that is also the name of a class referenced within that function. What about if you try:

function Update() {
    UpdateUI();
}

function UpdateUI()
 {
     HPLabel.GetComponent(UI.Text).text = HP + "/" + maxHP;
 }