- Home /
else if statement problem
Hello, I'm trying to create a script which decreases health dependant on whether Thirst or hunger are 0. It works fine, when either thirst or hunger = 0.
I'm trying to make this health degredation quicker if both are 0, as can be seen in my last "else if" statement, but never does anything. Is there another way I could write this set of statements?
/*DAMAGE HEALTH IF HUNGER OR FOOD IS ZERO*/
if(currentThirst <= 0)
{
currentHealth -= Time.deltaTime / 4;
}
else if(currentHunger <= 0)
{
currentHealth -= Time.deltaTime / 4;
}
else if(currentHunger <= 0 && (currentThirst <= 0))
{
currentHealth -= Time.deltaTime * 10;
Debug.Log("SPEED UP!");
}
Answer by oliver-jones · Sep 25, 2013 at 05:59 PM
Its just your ifs and else ifs are in the wrong order of statement. Lets say both your currentHunger and currentThrirst is at ZERO. The script will terminate only the first if statement because its technically true, thirst is at zero (as well as hungry, but its already called the if statement).
What you want it this:
if(currentHunger <= 0 && (currentThirst <= 0)){
currentHealth -= Time.deltaTime * 10;
Debug.Log("SPEED UP!");
}
else{
if(currentHunger <= 0)
currentHealth -= Time.deltaTime / 4;
if(currentThirst <= 0)
currentHealth -= Time.deltaTime / 4;
// ----- OR ----- \\
if(currentHunger <= 0 || currentThirst <= 0)
currentHealth -= Time.deltaTime / 4;
}
Your answer
Follow this Question
Related Questions
"Else" not working 2 Answers
In Game Animation 2 Answers
GUI Problem 2 Answers
PlayerPrefs Help 1 Answer
UCE0001: ';' expected. Inser a semicolon at the end 1 Answer