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

[83518-error.png|83518] Thank you for your reply. I received this error. Which ponits to the line: SceneManager.LoadScene (nextScene.name);

3 Answers

3

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)

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

This does indeed work. Would you have any idea what would be wrong with nextScenes.name? Or is there any better way that I could go about doing this? [83522-error.png|83522]

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 :slight_smile:

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?

I'm not familiar with next scene.name Is it your own code.