How I would write such a program to wait for something like a spacebar press?

I recently started using unity to make a platformer. My previous experience is mainly scratch. In scratch you can wait for an input before continuing your script.

I was wondering how I would write such a program to wait for something like a spacebar press ?

Hello @KandinGameCorporation, I too was (and still am somedays) a Scratch user. It has been awhile since I’ve been on the forum so my solution is probably not the most efficient, but for a newer user like you this should be ok:
I recomend using a Coroutine, these can be thought of as broadcasting a message in Scratch. I will put the documentation at the bottom.

So, to briefly explain before we go into code:

  1. We can call a Coroutine in our Update method.
  2. If we detect space pressed, we update bool variable.
  3. Then, in our update method, we carry on our code if the bool equals true.
//creating our variable
private bool boolVar;

void Start()
{
    //telling the coroutine to start
    StartCoroutine( Routine() );
}

void Update()
{
    //checking if our bool is true or not
    if( boolVar==true )
    {
        //code
    }
}

IEnumerator Routine()
{
    //BE CAREFUL WITH WHILE LOOPS, THEY CAN CRASH YOUR GAME AND EDITOR!!!!!!! ONLY USE IF YOU HAVE A CERTAIN LINE OF CODE, I'LL TELL YOU WHICH BELOW.
    while( boolVar==false )
    {
        //Checking for space input, then setting our bool if it's pressed.
        if( Input.GetKeyDown(KeyCode.Space) )
        {
            boolVar = true;
        }
        //THIS IS THE IMPORTANT CODE, IT ADDS A TIME DELAY PREVENTING CRASH.
        yield return new WaitForSeconds( 0.1f );
    }
}

Alright, thats it. Sorry if it’s confusing, I’m a little rusty, but I’ll be around if you need further help. :slight_smile:

Docs:

Thank you so much! This worked perfectly. I had heard about coroutines before but they always broke because I was missing the statement in the while loop. This fixed my problem

I am Bug123rules on scratch. I don’t have anything big, just a couple of side projects playing around with code before I got serious and used unity