Object reference - Object error in HealthDisplay.cs

I’m hoping someone else encountered the same error and can help.

I was following along with Sam in this video “Debug Health Display” and at the 9:30 time stamp he added the “String.Format” namespace, and completed the final format of line 18. When he goes back to play test the Health Display now shows the decrease in the players health in a full percent value (not a decimal value) anymore. I tried the same thing, but when I now start the game, I am getting a Object reference not set to an instance of an Object error in line 18 of the HealthDisplay.cs script.


I went back to look at the script and I don’t think I made any errors. I can’t determine where this missing reference is.

The big issue in the game now, is I can one punch enemies, and they can one punch me.

Did some play testing…
FIXED - the one punch issue. The progression levels seemed to have disappeared. So I added them back and now everyones hit points are normal. No more one punch death going on.
ERROR - Error ONLY happens once I attach the HealthDisplay script to the Health Value object under the HUD we created. Also, once added my health value doesn’t go down on the hud, yet my players HP’s are going down in the Inspector. If I don’t add it… Game runs fine. So still the same problem I cant seem to fix. :frowning:

Is the script on the same GameObject as the Text component?

Yup. I put it on the same GameObject Sam had his on. It made sense to be on the second one “Health Value” as that’s the percent health we wanted to be updated.

My Screen
image

Sams screen

I’m using TextMeshPro as I didn’t have just a text option.

Ah, the TextMeshPro version is a little different from the old Text version.
Be sure to include

using TMPro;

Then the component you’ll be looking for is

GetComponent<TextMeshProUGUI>()
1 Like

Yup. That did it Brian! Thank you so much.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms