- Home /
IF Statement Being Ignored
Hello everyone, I've come across a problem where one of my IF statement conditions is being ignored, and the code within it is being run even though the condition is false.
The purpose of these scripts is to: disable the player, turn the screen black, move the player to the spawn, and then allow the player to move.
The first code block contains the IF statement that is being ignored [if (screenFadeInOut.proceed == true)]. And the second block is the script that is primarily referenced by the first. Any insight is appreciated.
void IsPlayerDead ()
{
if (isDead || currentHealth<=0f)
{
playerMovement.enabled = false;
Debug.Log ("Player cannot move");
screenFadeInOut.sceneEnding = true;
Debug.Log ("Screen getting dark");
if (screenFadeInOut.proceed == true)
Debug.Log ("Ending scene now");
{
rigidbody.velocity = Vector3.zero;
Debug.Log ("The player has no speed");
transform.position = lastKnownSpawn;
Debug.Log ("Moving player");
isDead = false;
Debug.Log ("Player is now undead");
//screenFadeInOut.sceneStarting = true;
//Debug.Log ("Starting scene now");
playerMovement.enabled = true;
}
//disable controls, fade screen, reset position, reset room objects, turn screen back on, unlock controls
}
}
public void EndScene ()
{
if (sceneEnding == true)
{
guiTexture.enabled = true;
FadeToBlack();
if (guiTexture.color.a >= 0.95f)
{
guiTexture.color = Color.black;
//guiTexture.enabled = false;
sceneEnding = false;
proceed = true;
}
}
}
So are you saying that regardless of screenFadeInOut.proceed
being true
or false
, "Ending scene now" is being printed to the console?
Answer by CHPedersen · Jan 22, 2014 at 10:26 AM
Oh, I just realized something else which is probably the real cause. Notice that you must never put lines of code between an if-sentence and its associated block of code. The reason is that an if-sentence followed by a single line of code (not delimited by curly braces) will be interpreted by the compiler as that if-statement's block. I.e.:
if(someCondition == true)
Debug.Log("I am the if-statement's block");
Is equivalent to:
if(someCondition == true)
{
Debug.Log("I am the if-statement's block");
}
And this:
if(someCondition == true)
Debug.Log("I am the if-statement's block");
{
// Code here is not at all associated with the if-statement
}
Is equivalent to:
if(someCondition == true)
{
Debug.Log("I am the if-statement's block");
}
// Code here is not at all associated with the if-statement
To avoid this type of confusion in the future, always delimit all code for an if-sentence by curly braces {}, and do not put anything between the if-statement and its braces.
When used to just encapsulate otherwise free-standing code, curly braces instead *define a scope*, that is, a section of code with its own memory stack, which cleans up the memory allocated by variables declared inside of it when control flow leaves the scope. Declaring such a scope is what you have unknowingly done in your code above.
You are completely correct. The syntax I was using completely invalidated the scope of the script.
Answer by jaroosh · Jan 22, 2014 at 11:08 AM
Hey,
Ive just noticed, not sure if this is a typo here or its exactly like this in the code BUT :
if (screenFadeInOut.proceed == true)
Debug.Log ("Ending scene now");
{
what it does is : if the proceed is true, it will print Debug info and then regardless of the condition it will start a scope ({) and proceed withe the rest of the code. What you should to here is :
if (screenFadeInOut.proceed == true)
{
Debug.Log ("Ending scene now");
You and CHPedersen are correct, it was the syntax I was using.
Answer by Ebil · Jan 22, 2014 at 08:53 AM
I dont think this statement is being ignored, but you assume different results. Why dont you debug the values before and after the statement starts? Why do you check if player is dead or has < 0 HP? isdead should be the only value which is checked, anything else is no good way to handle this state.
I can see that [if (screenFadeInOut.proceed == true)] is false because I can watch it in the inspector (both the bool value and the alpha value of the guiTexture, and the code inside that block runs well before the condition even comes close to being achieved.
Also, I get what you are saying about isDead vs 0 HP, I wrote it that way because the player can take damage, but also be killed instantly. And rather than give those "instant kills" arbitrary high values, I just decided to make them trigger the bool directly.
.Net's runtime does not ignore if-sentences, and it does not evaluate conditions wrong. The bug is either in your code or in the way Unity serializes the value of screenFadeInOut.proceed to the Inspector.
Put screenFadeInOut.proceed in a call to Debug.Log just prior to the if-sentence. I'm positive it is indeed true when the if-sentence executes, and false when it doesn't.
I knew I was the problem, I just didn't see where I was causing it. Thank you to you and jaroosh for pointing it out to me.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
C# Character Controller Collision If Statement 2 Answers
C# if If Statement is True Declare Variable 1 Answer
Distribute terrain in zones 3 Answers