Too subjective and argumentative
Why is my if-statement still true when i set it to false, and wont move to the next else if-statement?
So when i start this, the code stays in the first if statement, even though i recognises the change of the boolean values. What am i doing wrong?
public class TrainingManager : MonoBehaviour {
public static float waitTimer = 2.0f;
private bool s_01_beginSequence = false;
private bool s_02_visualiseBtn = false;
private float waitBeforeStim = 1.00f; //Wait timers (amount of time)
private float waitBeforeRestart = 2.00f;
private float waitBeforeStimActual = 0.0f; //Wait timers (initial start time)
private float stimulate = 0.0f;
private float showButtonClr = 0.0f;
private float restart = 0.0f;
void Update ()
{
if ( s_01_beginSequence == true && s_02_visualiseBtn == false)
{
// wait until it is 1.0
if (waitBeforeStimActual < waitBeforeStim)
{
waitBeforeStimActual += Time.deltaTime;
}
if (waitBeforeStimActual > waitBeforeStim)
{
if (stimulate >= waitTimer)
{
stimSound.Stop ();
s_02_visualiseBtn = true;
s_01_beginSequence = false;
}
else if (stimulate < waitTimer)
{
stimulate += Time.deltaTime;
if (!stimSound.isPlaying)
{
stimSound.Play ();
}
}
}
}
else if ( s_01_beginSequence == false && s_02_visualiseBtn == true )
{
if (waitBeforeStimActual > waitBeforeStim)
{
if (showButtonClr >= waitTimer)
{
stimSound.Stop ();
s_02_visualiseBtn = false;
s_01_beginSequence = true;
stimulate = 0.0f;
showButtonClr = 0.0f;
}
else if (stimulate < waitTimer)
{
showButtonClr += Time.deltaTime;
if (!stimSound.isPlaying)
{
stimSound.Play ();
}
}
}
}
if ( Input.getKeyDown ("space") )
{
s_01_beginSequence = true;
}
it should be note that the s_01_beginSequence bool becomes true from another script
It is hard to say without seeing all the code. But I can assure you that if the script enters the first if
is because s_01_beginSequence
is true
and s_02_visualiseBtn
is false
.
So, what you're seeing is probably that you're not changing the values you think you are.
Can you post the code where the values are changed?
The value changes within the if-statement itself; in the nested if statement: if (stimulate <= waitTimer) {..} Line 25. And these changes are made when the "stimulate" variable is increased to a specific value. (2 seconds i think).. The else if statement on line 43 does the same as its if-statement above, just the opposite
Answer by DenisTribouillois · Jul 13, 2016 at 01:16 PM
private static bool s_01_beginSequence = false;
If it becomes true from another script, you should change the to public:
public static bool s_01_beginSequence = false;
You can make sure your variable is correctly set by printing it just before the if statement:
print(s_01_beginSequence);
if ( s_01_beginSequence == true && s_02_visualiseBtn == false)
the "private static" is me mis-typing when i had to rewrite it in the question. It is public :). Im using the Debug.Log, and the values are visually changing when the timer (stimulate += Time.deltaTime; ) lets the code enter the if-statement in line 25. But the code does not continue to the next "else if"
Answer by rmassanet · Jul 15, 2016 at 07:54 AM
I've tested your code and it does not stay in the first if
statement. When the time limits are reached it proceeds to the else
statement.
However, there is a very rare case in which your code could get stuck in the first if
statement. That remote possibility is when waitBeforeStimActual == waitBeforeStim
. If that was the problem, it can easily be fixed by changing one of the two inner if
statements, to cover the equality case. Although remote, it is worth covering the case and avoid spurious application fails.
I hope this helps.
In your test, did it even go back and forth between both statements in a loop? If i change the else-if to a normal if statement, I am able to make it go through both statements, but only once - and then nothing happens
I didn't check that because you said the problem was that it did not leave the first if
statement.
The answer is no it never goes back, because the code never tells it to go back:
else if ( s_01_beginSequence == false && s_02_visualiseBtn == true )
{
if (waitBeforeStimActual > waitBeforeStim) {
}
}
Note that when the code enters the else if
statement waitBeforeStimActual
is already larger than waitBeforeStim
, and so, obediently, it does nothing.
Answer by Funkeeh · Jul 15, 2016 at 09:26 AM
Just for an update: I re-wrote the code and used a switch-case instead of if the if - else-if statements, and that seemed to work. So Switch-case 1 - 0 if-statements :)
That doesn't make any sense. It's not if-else
's fault. When you rewrote the code you probably fixed an error. Can you post your working code?