Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by MiniMurgle · Jun 03, 2017 at 05:02 AM · uiguitexttostringhealth

Showing player health on UI

Show I've set up my player health using floats as that's how I know how to do it. But now I'm trying to implement UI. Mainly just the player health. I don't want to display it in some fancy form like a bar just a little cur/max in the the top left.

My problem lies in getting the health to show on the UI. I currently have the code set up as is.

 public float playerCurHealth = 25;
     public float playerMaxHealth = 25;
     public float playerKnockback = 10;
     Text playerhealth;
 
     // Use this for initialization
     void Start()
     {
         playerhealth = GetComponent<Text>();
         
     }


After about an hour of google and me fiddling around I've decided that this works if my player health is Text already but that's not how I have it set up. I've tried using to string like this

 playerhealth = playerCurHealth.ToString;


But unfortunately this doesn't work either.

Does anyone know what I'm missing? I'm sorry that this is probably a stupid question, but thank you for your time and consideration.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Reals7eel · Jun 03, 2017 at 07:44 AM

if im correct about you wanting your health to show like this for example: "90/100" in Text, then you just want to do something like this (): since you're using floats, they'll show as decimals, which i assume you dont want, so do this:

 playerhealth.text = "" + Mathf.RoundToInt(playerCurHealth) + "/" + Mathf.RoundToInt(playerMaxHealth);

instead of:

 playerhealth = playerCurHealth.ToString(); 


because what you were trying before (^^ above) acts as you changing the variable, and not what it displays.

Let me know if this works, and if you have any other problems.

Comment
Add comment · Show 6 · 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 MiniMurgle · Jun 03, 2017 at 03:32 PM 0
Share

Thank you for the advice you've given so far but I still haven't managed to get it working. I've moved the code into a separate script attached too the text as I feel that makes more sense. This is my current code.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class HealthGUI : $$anonymous$$onoBehaviour {
 
     Text playerHealth;
 
     // Use this for initialization
     void Start () {
 
         playerHealth = GetComponent<Text>();
         playerHealth.text = "" + $$anonymous$$athf.RoundToInt(gameObject.GetComponent<PlayerStats>().playerCurHealth) + "/" + $$anonymous$$athf.RoundToInt(gameObject.GetComponent<PlayerStats>().player$$anonymous$$axHealth);
 
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }
 

I added the getcomponent into the RoundToInt because otherwise it didn't know what I was referencing. At the moment I have the textbox I wish to display this to set up with a default text that just says "Player Health" for refrence. So when I run the game I wanted it to display like you mentioned. 90/100, or more specifically $$anonymous$$e would be 20/25. So my problem now is that the text box itself does not change when I run the game. Thank you again for your time.

avatar image Reals7eel · Jun 03, 2017 at 06:48 PM 1
Share

@$$anonymous$$ini$$anonymous$$urgle sorry for late reply. the display of your current health isnt updating, you put it in Start() which only calls on the first frame of the game, if you want it to always update, put the

 playerHealth.text = "" + $$anonymous$$athf.RoundToInt(gameObject.GetComponent<PlayerStats>().playerCurHealth) + "/" + $$anonymous$$athf.RoundToInt(gameObject.GetComponent<PlayerStats>().player$$anonymous$$axHealth);

in the Update() void, that should work for ya :D and no problemo! also, set the "Text playerHealth;" to "public Text playerHealth;" that way you can remove the playerHealth = GetComponent(); and just drag the text into the variable spot in your inspector

avatar image MiniMurgle Reals7eel · Jun 03, 2017 at 07:08 PM 0
Share

Really sorry to have to keep bugging you about this. But still having one issue. I moved the line of code to update as you suggested.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class HealthGUI : $$anonymous$$onoBehaviour {

 Text playerHealth;

 // Use this for initialization
 void Start () {
     playerHealth = gameObject.GetComponent<Text>();
 }
 
 // Update is called once per frame
 void Update () {
     playerHealth.text = "" + $$anonymous$$athf.RoundToInt(gameObject.GetComponent<PlayerStats>().playerCurHealth) + "/" + $$anonymous$$athf.RoundToInt(gameObject.GetComponent<PlayerStats>().player$$anonymous$$axHealth);
 }

}

But it still doesn't update. It does throw a null reference error at me though.

"NullReferenceException: Object reference not set to an instance of an object HealthGUI.Update () (at Assets/Scripts/Utils/UI/PlayerGUI/HealthGUI.cs:17)"

Line 17 is the line of code in update. I have no idea what part of it the error wants though. As far as I can tell everything is set fine.

avatar image Reals7eel MiniMurgle · Jun 03, 2017 at 10:00 PM 1
Share

first, where you have: Text playerHealth; make it public Text playerHealth; remove the playerHealth = gameObject.GetComponent<Text>(); then after you save and compile the script, go to the gameobject in the hierarchy that has that script on it, you should see it say "player Health" then in the box next to it it will say "None(Text)" or something like that, if you go into your hierarchy and find the text you want to use, grab it with your mouse, and drag it into the "None(Text)" box on the script in the inspector. hope this helps!

Show more comments
Show more comments

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

177 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is this List only showing one int? 0 Answers

How do I let users change the background color of a selected word in an input field? 0 Answers

How to make GUI variables compatible with the UI system? 0 Answers

TextUI text changes size if resolution changes 1 Answer

TextMeshPro Set Alpha Color 0 Answers


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