- Home /
The question is answered, right answer was accepted
Health Bar Percentages Problem
Hi! I'm having trouble with my health bar percentage and I don't know why because I divided them and then multiplied them by 100.
Here take a look. (says 50/100 and 0%)
It really doesn't make since to me because if I set it to 100 then it is fine.
Here is the code:
using UnityEngine.UI;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public Image PlayerHealthBar;
public Text Fraction;
public Text Percent;
public int maxHealth = 0;
public int curHealth = 0;
// Use this for initialization
void Start ()
{
Fraction.text = curHealth + "/" + maxHealth;
Percent.text = (curHealth/maxHealth) * 100 + "%";
}
// Update is called once per frame
void Update ()
{
}
}
And here is the setup of it
Well anyways hope someone can help, thanks in advance.
Try
((curHealth/maxHealth) * 100).ToString() + "%";
Also, are you entereing those values in inspector after you click play? The code is in start so it runs pretty much after you've clicked go?
I tried it but it didn't work still says 0% even though it should say 50%, and no I didn't touch the values after I click play.
Your inspector values could be overriding your script. They say 100/50 while the script initialises to 0.
Answer by Baste · Mar 04, 2015 at 02:04 PM
When you do this:
curHealth / maxHealth
, you're dividing one integer by another integer, and you're getting an integer returned. In C# (and most other languages), integer division rounds down to the closest integer result. So 7 / 3 = 2. In your case, 50/100 should be .5, but it's rounded down to 0. For more information, check out the official docs here.
To fix this, you'll have to force float (decimal) division. The easiest way to do that is to convert one of the values into a float, as float division will be used if either the dividend or the divisor is a float:
Fraction.text = curHealth + "/" + maxHealth;
float curHealthFloat = curHealth;
Percent.text = (curHealthFloat/maxHealth) * 100 + "%";
EDIT: you should also replace these lines:
public int maxHealth = 0;
public int curHealth = 0;
with this:
public int maxHealth;
public int curHealth
As setting values in the script that you're overriding in the inspector can have weird effects when you start working with prefabs. The default value for integers are 0 anyways, so setting them to 0 doesn't really do anything useful. You're either overriding some value you have set in the inspector, or doing nothing.
Answer by curiouspers · Mar 04, 2015 at 04:36 PM
Your max and cur health is int, that's why you get 0, there is no error. 50/100 = 0.5, but only if result is float, you have int result, so it's 0
Use this:
Percent.text = ((float)curHealth/maxHealth) * 100 + "%"
this will convert curHealth to float, and output will be float
Follow this Question
Related Questions
How to convert unity editor GUI functions to something that can be used in game? 0 Answers
How to scale triangle created by GL(using viewport position) with scale factor of Canvas? 0 Answers
Converting OnGUI function to UI 1 Answer
How do I add something to the OnClick of a UI Button from an Editor-Script? 0 Answers
Multiple Cars not working 1 Answer