# WaitForSeconds problem (545755)

**URL:** <https://discussions.unity.com/t/waitforseconds-problem-545755/545755>\
**Category:** C# & .NET\
**Tags:** Scripting\
**Created:** [August 2, 2014, 3:17pm UTC](https://discussions.unity.com/t/waitforseconds-problem-545755/545755 "2014-08-02T15:17:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Onithec4t](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/onithec4t/32/438651_2.png) [@Onithec4t](https://discussions.unity.com/u/Onithec4t)\
**Post date:** [August 2, 2014, 3:17pm UTC](https://discussions.unity.com/t/waitforseconds-problem-545755/545755/1 "2014-08-02T15:17:01Z")

</div>

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 :

```csharp
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?

---

<div class="post-metadata">

**Author:** ![LeftyRighty](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@LeftyRighty](https://discussions.unity.com/u/LeftyRighty)\
**Post date:** [August 2, 2014, 3:39pm UTC](https://discussions.unity.com/t/waitforseconds-problem-545755/545755/2 "2014-08-02T15:39:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Onithec4t](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/onithec4t/32/438651_2.png) [@Onithec4t](https://discussions.unity.com/u/Onithec4t)\
**Post date:** [August 2, 2014, 3:41pm UTC](https://discussions.unity.com/t/waitforseconds-problem-545755/545755/3 "2014-08-02T15:41:26Z")

</div>

The ball should wait and after that, moves.

---

<div class="post-metadata">

**Author:** ![der\_r](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/der_r/32/400264_2.png) [@der\_r](https://discussions.unity.com/u/der_r)\
**Post date:** [August 2, 2014, 3:53pm UTC](https://discussions.unity.com/t/waitforseconds-problem-545755/545755/4 "2014-08-02T15:53:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dantus](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dantus/32/148077_2.png) [@Dantus](https://discussions.unity.com/u/Dantus)\
**Post date:** [August 2, 2014, 3:57pm UTC](https://discussions.unity.com/t/waitforseconds-problem-545755/545755/5 "2014-08-02T15:57:33Z")

</div>

> [@LeftyRighty](#):
>
> Coroutines are there to do exactly this, start something in a separate thread… what are you trying to achieve with this code?

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.

---

<div class="post-metadata">

**Author:** ![Onithec4t](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/onithec4t/32/438651_2.png) [@Onithec4t](https://discussions.unity.com/u/Onithec4t)\
**Post date:** [August 2, 2014, 4:06pm UTC](https://discussions.unity.com/t/waitforseconds-problem-545755/545755/6 "2014-08-02T16:06:30Z")

</div>

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