1

I am try to update GUI during Update(). Here is the code:

void Update()
    {
        Debug.Log(HPText == null);
        HPText.text = HP + " / " + MaxHP;
    }

I have already set the HPText through Inspector.
enter image description here

And I get the following in the console. It seems that it keep alternating between null and not null. How can I get rid of the error?
enter image description here

4
  • 4
    Is it possible you have more than one object running this script? Commented Aug 19, 2015 at 9:11
  • The information you have given is not enough. Include more codes from your class like how are you declaring HPText or screenshot of you project hierarchy. Commented Aug 19, 2015 at 9:49
  • @MickyDuncan Thanks, you are right. I have 2 object using the same script. I forget to disable the one I am not using. Commented Aug 19, 2015 at 11:12
  • You're quite welcome good sir Commented Aug 19, 2015 at 13:36

1 Answer 1

3

Note: This is a summary answer based on my comment above.


Sounds like you have more than one object running this script. Check your scene and remove the script from the second object. One object has the HPText property set, the other more than likely does not.

Thanks, you are right. I have 2 object using the same script. I forget to disable the one I am not using. – Joshua

Alternatively, you could set the HPText on the second object. You mentioned you have since disabled the second object - that works too.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.