boolean not working right

boolean is supposed to stop the player from being able to input the keycode “moveUp” or “moveDown” for 1 second because i said its “false” then wait for one second but it still continues being true and there isnt a one second delay once the reset players function is called. This is in java script can anyone help me fix it?

var playersAreReady : boolean = true;

var moveUp : KeyCode;

var moveDown : KeyCode;

var speed : float = 15;

function ResetPlayers ()
{

transform.position.y = 0;

playersAreReady = false;

	yield WaitForSeconds(1);

	playersAreReady = true;
}
	 function Update () 
	{
	if  (playersAreReady == true);
	(Input.GetKey(moveUp));

	}
		rigidbody2D.velocity.y = speed;

	  if (playersAreReady == true);
	 (Input.GetKey(moveDown));
	{
		rigidbody2D.velocity.y = speed *-1;
	}

1 Answer

1

For future posts, please format your code. After pasting, select your code and use the 101/010 button. I did it for you.

The issue with your code is the ‘;’ at the end of lines 22 and line 28. These are terminating the ‘if’ condition. If you remove the ‘;’ your code will work.

It still does not reckgonize that the boolean is false and it has to wait for one second but it doesnt wait for any ammount of time

It doesnt reckgonize that when the function "ResetPlayers" is called that the boolean is false for one second it continues without waiting for one second.

Nothing in your code calls ResetPlayer(), so nothing is reseting 'playerAreRead'. When debugging a problem line this, start with something simple. Just have a function called in start and verify the values are changing.

I have another code that calls it but when it is called it only resets the y position to 0 but it does not call the playersAreReady false

I'd have to see the rest of the script and how things are being called. As a guess, you are likely calling ResetPlayers() from inside of Update() or some other function that gets called a number of frames. The result is that you stack up a number of coroutimes. But that is only a guess.