WaitForSeconds problem

Hi guys.
I started learning unity using C# yesterday, but I know something about programming (with java).

To learn, I’m trying to develop a simple pong game, but I’ve some trouble with WaitForSeconds.
I think it can be related to the multithread engine.
That’s what i’m doing :

public class BallControl : MonoBehaviour {

    bool wait=false;
   
    void Start (){
        if (!wait) {
            Debug.Log(wait);
            StartCoroutine(hi (5.0f));
            Debug.Log(wait);
        }
        move ();
    }

    IEnumerator hi(float sec){
        Debug.Log("wait for "+sec+" seconds");
        yield return new WaitForSeconds(sec);
        Debug.Log("waited for "+sec+" seconds");
        wait = true;
        }
    void move(){...}
}

When I start the game, the ball does not wait, but the console log is this:

false
false
wait for 5 seconds
waited for 5 seconds

The hi method is called but it seems to be a separated thread.
Any suggestions?

Coroutines are there to do exactly this, start something in a separate thread… what are you trying to achieve with this code?

The ball should wait and after that, moves.

Put the call to move() into your coroutine at the very end.

1 Like

Coroutines are executed in the main thread. They are not multithreaded at all.

der_r just gave you the correct hint. move() has to be in the coroutine, immediately after the waiting. I don’t know exactly what the wait flag is supposed to do. As far as I can see it is not needed at all, but maybe you wanted it to do something else.

the der_r solution works well, ty.
The wait flag was just for debugging, not other uses.