- Home /
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;
}
Answer by troien · Mar 11, 2016 at 10:20 AM
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 :p 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.
Answer by SaurabhStudio · Mar 11, 2016 at 02:53 PM
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
Answer by See_Sharp · Mar 11, 2016 at 09:31 AM
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.
Answer by tanoshimi · Mar 11, 2016 at 10:42 AM
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;
}