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.
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.
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.
@$$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
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.
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!
Your answer
Follow this Question
Related Questions
TextUI text changes size if resolution changes 1 Answer
TextMeshPro Set Alpha Color 0 Answers
Simple Game Over script 2 Answers
GUI Text not showing up :( 7 Answers
UI Text Editing Problem, What's Wrong? Need Anybody to Assist 1 Answer