- Home /
rect.width gets set to 0 for unknown reason
So i'm having an odd issue with my health bar. To try and decrease health, what i did was use two textures one on top of another. The front is the health, the back is the background
to show a loss of health, I divided the current health and the max health, then multiplied it by the width of the background, so it becomes a percentage of the background width. But in the editor when playing my game, once the current health becomes anything below the max health it automatically goes to width 0, displaying as if there's no health!
Hopefully that makes sense, if not i'll try to elaborate
Here's my code:
public int maxHealth = 100;
public int curHealth = 100;
public Texture2D healthBarFront;
public Texture2D healthBarBack;
public float healthBarWidth = 80;
public float healthBarHeight = 8;
public float above;
Rect rect;
Rect rectFront;
public float rectFrontWidth;//This is just used for me to view in the inspector
// Use this for initialization
void Start () {
rect.width = healthBarWidth;
rect.height = healthBarHeight;
}
// Update is called once per frame
void Update () {
rectFrontWidth = (float)rectFront.width; //Again, just for inspector
}
//Drawing healthbar
public void OnGUI() {
//finding the position
Vector3 v3pos;
v3pos = transform.position;
v3pos.y += above;
v3pos = Camera.main.WorldToScreenPoint(v3pos);
v3pos.y = Screen.height - v3pos.y;
//Transffering into a Rect (The background)
rect.x = v3pos.x - healthBarBack.width /2;
rect.x += xoffest;
rect.y = v3pos.y - healthBarBack.height /2;
rectFront = rect;
//This next line is where the problem is. Width gets set to 0 if curHealth is less than maxHealth
rectFront.width = (curHealth/maxHealth) * healthBarWidth;
GUI.DrawTexture(rect, healthBarBack);
GUI.DrawTexture(rectFront, healthBarFront);
}
Answer by Eric5h5 · Jun 10, 2013 at 03:49 AM
You're using integer division; either use floats instead of ints, or else cast to float when doing the division.
in other words
rectFront.width = (Float(curHealth)/maxHealth) * healthBarWidth;
or
rectFront.width = (curHealth/int(maxHealth)) * healthBarWidth;
rectFront.width = (float)((curhealth/maxhealth) * healthbarWidth)
@$$anonymous$$ukabr: the function style cast operator doesn't exists in C# as far as I know. It belongs to C or C++.
@bodec: Your cast won't help since you just cast the final result which will be 0.
When you combine those two comments you get:
rectFront.width = ((float)curhealth / maxhealth) * healthbarWidth;
It's all about the division operator. At least one operand need to be a float so the compiler does a float division.
Note: UnityScript doesn't have the c-style cast, so you have to use implicit casting by using a temporal float variable for one of the operands.
Or you can use parseFloat, or you can add 0.0, as in (curhealth + 0.0).
Oh my god i feel dumb, it was that obvious! the float cast worked perfectly,
rectFront.width = ((float)curHealth/maxHealth) * healthbarWidth;
Thanks a lot guys! Now my health bar system is all set up!
thanks Eric5h5 for your explanation, and bunny83 for your example!
And thanks to everyone else for your input!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help With Enemy Health bars 3 Answers
GuiTexture above 3D objects 1 Answer
I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer
Player Heal *Help* 0 Answers