- Home /
if statement with floats in C# . what did i do wrong ?
So i wrote a simple if statement with float variables in C# that simply doesn't work i think i wrote everything right and i have no clue what's wrong . here's a screenshot of the code : http://oi57.tinypic.com/2u7vb4i.jpg . Thanks for your time
define "wrong".
it'll execute more than it should because you're checking for less than or equal to zero when just less than is enough. otherwise it's doing what you told it to.
wrong is also posting a screenshot ins$$anonymous$$d of the actual code - it's easier for us to help you if you post the relevant part of the script. (don't forget to use code tags - the faq/tutorial video will help explain things).
it doesn't execute the code that's inside the {} while the statement is true that's what's wrong .
There isn't anything wrong with your if statement. Try adding some debugging statements that you can read in the console:
//... previous code
void Update() {
HealthBarScale -= 0.005f;
Debug.Log(string.Format("In PlayerInfo Update method, Current Value of HealthBarScale: {0}", HealthBarScale));
if (HealthBarScale <= 0.00f)
{
Debug.Log(string.Format("HealthBarScale is less than or equal to 0.00, its current value is: {0}, forcing 0.00f as HealthBarScale value.", HealthBarScale));
HealthBarScale = 0.00f;
}
else
{
Debug.Log(string.Format("HealthBarScale is greater than 0.00, its current value is: {0}, no changes to variable at this time.", HealthBarScale));
}
healthBar.localScale = new Vector3(HealthBarScale, 0, 0);
}
You should also look into using $$anonymous$$athf.Clamp to clamp the value to 0.
Also if you HealthBarScale -= 0.005f;
in every update() it ll be 0 form the first 2 frames... and it will go - really fast;
Your answer
Follow this Question
Related Questions
Error when checking if float is a whole number 2 Answers
How do you keep spheres from floating into space? Rigidbody not working. 0 Answers
How do I make my own Remote Settings backend like Unity Analytics Remote Settings? 0 Answers
Can't change a float 2 Answers
Can I create a list with an int/float and a string? C# 2 Answers