- Home /
Converting Int to String results in NullReferenceException?
Hey Again UnityAnswers.
I'm once again facing a weird problem. I want to display my currentHealth variable on the healthText.text HUD element. Now, my currentHealth is an integer. I've tried converting it to a string before changing the UI using ToString();, but this only returns a NullReferenceException error. Now, i know that NullReferenceException means that currentHealth is null, but in Awake() i set currentHealth to startingHealth, which is 100...
Here's my code. Thanks in advance!
public void TakeDamage (int amount)
{
// Set the damaged flag so the screen will flash.
damaged = true;
currentHealth -= amount;
currentHealth.ToString ();
// Set the HUD health string's value to the current health
healthText.text = currentHealth;
if(currentHealth <= 0 && !isDead)
{
// ... it should die.
Death ();
}
}
without seeing the type of currentHealth
it's impossible to confirm that's where the NRE comes from. if it's a float/int
then that's not the case though. most likely, it's where you're trying to assign the value to healthText.text
because healthText
is null
.
try adding some Debug.Log
messages to display the values of those items and when postig errors, try including the FULL error text. it'll help others help you.
Thanks for replying.
It's where you're trying to assign the value to healthText.text because healthText is null. healthText is a UI Text element in my scene, and it is 100 by default.
try adding some Debug.Log messages to display the values of those items Tried to Debug.Log the currentHealth, which just returned the value of my health, and when i got hit, it decreased properly. Also tried debugging healthText.text, which returned NullReferenceException.
what about Debug.Log(healthText);
?
when debugging start at the higher level - if healthText
is null
, then you'll always get the NRE for healthText.anything
Im feeling really, really stupid right now....
I forgot to assign healthText. Thanks for your help buddy, sorry for taking your time.
Answer by Arkamis · Jun 21, 2015 at 11:18 AM
Integer types are not nullable, unless you make them so.
Therefore, you have one of two problems:
currentHealth
is not an integer, but some other nullable type, and it is currently null.health
, orhealth.text
is null.
#2 is the more likely scenario.
In any case, simply calling currentHealth.ToString()
doesn't make currentHealth
a string -- you need to assign it.
Try the following code, which affords you some robustness:
if (health != null)
health.text = currentHealth.ToString();
else
throw new NullReferenceException("Variable health is not set.");
Answer by RobotReebot · Jun 20, 2015 at 06:40 PM
Maybe i am wrong but try:
healthText.text=currentHealth.ToString ();
I am not sure but it should work
Tried this too, returned NullReferenceException. Thanks for taking your time though. (:
i see that you're trying to help, but please only post answers if you're sure... if you're not then you're not really adding value to the question and maybe causing more confusion.
Your answer
Follow this Question
Related Questions
Changing a GUI String to read as a INT 2 Answers
Problem with Bullets on GUI 1 Answer
Parsing a String to a Color 1 Answer
int to char conversion in JS ? 2 Answers
Getting string to int then var 1 Answer