Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Dellitsni · Jun 20, 2015 at 06:26 PM · guinullreferenceexceptionstringint

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 ();
         }
     }



Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Jun 20, 2015 at 06:32 PM 0
Share

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.

avatar image Dellitsni · Jun 20, 2015 at 06:39 PM 0
Share

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.

avatar image gjf · Jun 20, 2015 at 06:43 PM 0
Share

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

avatar image Dellitsni · Jun 20, 2015 at 06:45 PM 0
Share

Im feeling really, really stupid right now....

  • I forgot to assign healthText. Thanks for your help buddy, sorry for taking your time.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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:

  1. currentHealth is not an integer, but some other nullable type, and it is currently null.

  2. health, or health.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.");


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dellitsni · Jun 20, 2015 at 06:42 PM 0
Share

Tried this too, returned NullReferenceException. Thanks for taking your time though. (:

avatar image gjf · Jun 20, 2015 at 06:44 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges