3

I need to create a infinite loop in Unity without using the main thread? I saw some example, but it's not usefull:

while(true){
         var aa;
         debug.log("print.");
} 

I want to add some delay like e.g. 2 seconds. If anybody knows the solution please help.

derHugo
  • 83,094
  • 9
  • 75
  • 115
riaz hasan
  • 1,155
  • 2
  • 8
  • 20
  • 3
    Coroutines are way to go :) – Łukasz Motyczka Dec 12 '16 at 12:41
  • 1
    The best thing for you to use is probably coroutines.. with WaitForSeconds(2). Remembering that the Unity game engine already does a loop. – Rafael Costa Dec 12 '16 at 12:41
  • 1
    If you only want to make some simple operation then coroutines are okay. But i you would like to execute some heavy operation eg. Read large binary/text file ( 100+mb ) then coroutines will fail ( in most cases ) – mrogal.ski Dec 12 '16 at 13:07

3 Answers3

11

First define a Coroutines:

private IEnumerator InfiniteLoop()
{
    WaitForSeconds waitTime = new WaitForSeconds(2);
    while (true)
    {
        //var aa;
        Debug.Log("print.");
        yield return waitTime;
    }
}

Then call it like that:

 StartCoroutine(InfiniteLoop());

Added note:

If you happen to change Time.timeScale and don't want that to affect delay time, use:

yield return new WaitForSecondsRealtime(2);
Programmer
  • 121,791
  • 22
  • 236
  • 328
Ludovic Feltz
  • 11,416
  • 4
  • 47
  • 63
  • 2
    This kind of Coroutine is not healthy for any application. It will drive GC crazy. as you are creating a `YieldInstruction` every iteration. Would be better if you create an instance of WaitForSecond before while loop. – Umair M Dec 12 '16 at 12:45
  • Invoke is better, where is @JoeBlow when you need him? – Catwood Dec 12 '16 at 12:47
  • @UmairM I just followed the Unity documentation https://docs.unity3d.com/ScriptReference/WaitForSeconds.html There is always some way to optimize this code if you feel that performances are an issue – Ludovic Feltz Dec 12 '16 at 13:10
  • @Ludovic Yes. Apparently Unity docs are not using while statement in that example. Performance is a big issue with coroutines, specially when they contain infinite loop. Here is a [video](https://www.youtube.com/watch?v=sUYN8XtuUFA) which explains it better. – Umair M Dec 12 '16 at 14:18
  • @UmairM My bad, in this page of the documentation it does not use while statement but in the [StartCoroutine page](https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html) it does – Ludovic Feltz Dec 12 '16 at 14:30
  • @UmairM Interesting video by the way :) – Ludovic Feltz Dec 12 '16 at 14:35
  • 2
    @UmairM Check the fixed answer. `WaitForSeconds` should be declared outside the while loop and GC problems will be gone. – Programmer Dec 12 '16 at 18:02
  • I think `InvokeRepeating` is better. – fmnijk Aug 12 '21 at 19:46
3

Use this to create the loop;

private IEnumerator LoopFunction(float waitTime)
{
    while (true)
    {
        Debug.Log("print.");
        yield return new WaitForSeconds(waitTime);
        //Second Log show passed waitTime (waitTime is float type value ) 
        Debug.Log("print1.");
    }
}

For calling the function, don't use Update() or FixedUpdate(), use something like Start() so you don't create infinite instances of the loop;

 void Start()
 {
      StartCoroutine(LoopFunction(1));
 }
zvava
  • 101
  • 1
  • 3
  • 14
Shitul
  • 136
  • 1
  • 4
0

Use coroutines..

//Call in your Method
StartCoroutine(LateStart(2.0f));

Then write coroutine like..

private IEnumerator LateStart(float waitTime)
{
    yield return new WaitForSeconds(waitTime);

    //After waitTime, you can use InvokeRepeating() for infinite loop infinite loop or you use a while(true) loop here
    InvokeRepeating("YourRepeatingMethod", 0.0f, 1.0f);
}

Here is the documentation for InvokeRepeating(): https://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html

Moti
  • 9
  • 2