My boolean is not being called

Yes i have something that calls the function “ResetPlayer” My boolean which is “playersAreReady” is not being called to be true or false in the update"

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;

what do you mean by is not being called

(Input.GetKey(moveUp)); (Input.GetKey(moveDown)); are both varriables in unity and i set them to certain keys such as w and s playersAreReady its suposed to check whether its true or false

(Input.GetKey(moveUp)); returns a boolean, but you're not assigning the result to anything, so it's pointless. playersAreReady is only checking for true, not true or false, as I pointed out. And you still haven't addressed the fact that you have semicolons following the conditional statements, AND I've just noticed that your brackets are unbalanced, so I'm surprised this script is compiling at all.

4 Answers

4

Here’s my best guess at what you intended to write in your Update() function:

function Update ()
{
    if (playersAreReady == true) {

      if(Input.GetKey(moveUp)){
          rigidbody2D.velocity.y = speed;
      }

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

    }
}

yes but my boolean isnt recgonizing if its true or false

tanoshimi code is the best written best guess for what you need. If you are still having the Boolean problem then that issue is probably caused somewhere else. I would accept his answer and start a new question when you have a better idea where the problem might be.

@tanoshimi: I would suggest to add another else at the end of your else-if-chain and set the velocity to 0 there. Otherwise once a button is pressed the rigidbody will either move up or down and can't stop. Maybe that's what the OP want, but it seems a bit strange.

Is the reset players function being called in the function update. It may be being called more than once and not have time to wait for seconds.nuse a for loop to make sure it is called once.

reset players function is being called by another script

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

i want it to be if playersAreReady is false i want the (Input.GetKey(moveUp)) to not move i want it to only move if playersAreReady are true.

is that what you were saying?

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

yes but my boolean isnt recgonizing if its true or false

what do you mean by not recognizing , where you exactly set it in first place that you want it to remembers it

var moveUp : KeyCode;
var moveDown : KeyCode;
var speed : float = 10;

function start () {
playersAreReady = true ; 
}

function ResetPlayers () {
 
    transform.position.y = 0;
 
    playersAreReady = false;
    Debug.Log("Players are ready is: " + playersAreReady);
 
    yield WaitForSeconds(2);
 
    playersAreReady = true;
        Debug.Log("Players are ready is: " + playersAreReady);

}
 
function Update ()
{

	if  (playersAreReady == true ){
	
    if (Input.GetKey(moveUp))    
        
        rigidbody2D.velocity.y = speed;
     else 
     	rigidbody2D.velocity.y = speed *0;
     }
   
   	if  (playersAreReady == true ){
    
    if (Input.GetKey(moveDown))

        rigidbody2D.velocity.y = speed *-1;
     else 
     	rigidbody2D.velocity.y = speed *0;
    }
    	
    	if  (playersAreReady == false ){
    if (Input.GetKey(moveUp))    
        
        rigidbody2D.velocity.y =speed *0;
   
     if (Input.GetKey(moveDown))

        rigidbody2D.velocity.y =speed *0;

    }
}

for some reason my keycode "moveUp" will not work now and that is all i need for this code to be complete

Can anyone tell me why my keycode moveUp will not owrk anymore only moveDown is working

I did put the debug.log message and the booleans are working correctly

ok so what i want is if the moveUp key and the moveDown key is NOT pressed i want the rigidbody2D.velocity.y speed to equal zero