1

I just want a simple way to delay a while loop. I'm using C#. Here's my code:

while (value<101)
{
    Debug.Log(value);
    value = value + 10;
    //delay here (that i don't know how to do)
}

I used a bit of google translate so the text might be bad.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • Why? You shouldn’t try to block things – Daniel A. White Jul 20 '23 at 01:24
  • @dmedine I would urge against using `Thread.Sleep()` in Unity since it will pause the entire game and freeze other scripts (unless that is what you want). – SetupWizard Jul 20 '23 at 01:38
  • @dmedine well yes of course `Thread.Sleep` is a generic way to pause a thread entirely. The thing is that you do certainly not want to pause the UI main thread of an application ;) And in Unity unless you explicitly tell it different by default everything runs on the main thread and additionally most API is only available on the main thread ;) – derHugo Jul 20 '23 at 07:29
  • What exactly are you trying to achieve? You might not need a loop at all for this - have in mind that there is `Update` which runs once a frame so you could probably also use a simple counter that increases a bit every frame and once it exceeds a certain value (your desired delay) you increase the `value` – derHugo Jul 20 '23 at 07:32
  • @dmedine - If you're using `Thread.Sleep` to pause a loop in .NET then you are probably doing something wrong. – Enigmativity Jul 21 '23 at 04:58
  • OK, I deleted the comment. Everyone can stop pointing out that it was wrong now. – dmedine Jul 21 '23 at 06:09
  • @Enigmativity when I am polling hardware devices to see if new data is available on a background thread, I use Thread.Sleep all the time. That is not wrong. I understand that it is wrong to do it in Unity. – dmedine Jul 21 '23 at 06:11

2 Answers2

4

Unity allows you to delay code execution using coroutines. This avoids pausing the entire thread, unlike Thread.Sleep(). To add a delay to a while loop you could do something like this:

private IEnumerator DelayedWhileLoop()
{
    int value = 0;
    while (value < 101)
    {
        Debug.Log(value);
        value += 10;
        yield return new WaitForSeconds(1); // Delay for 1 second
    }
}

void Start()
{
    StartCoroutine(DelayedWhileLoop());
}
SetupWizard
  • 119
  • 8
0

you can do this using Unity's Coroutine function with delay. Please run the beloved code. public class LoopWithDelayExample : MonoBehaviour { private bool isRunning = false;

    // Start Coroutine
    public void StartCoroutineTest()
    {
        if (!isRunning)
        {
            StartCoroutine(DelayCoroutineWithLoop());
        }
    }

    // Delayed Coroutine
    private IEnumerator DelayCoroutineWithLoop()
    {
        isRunning = true;
        float loopingTime = 5f; // The duration of the loop in seconds.
        float delay = 2f; // The delay duration in seconds.

        float startTime = Time.time;
        float endTime = startTime + loopingTime;

        // Loop  for given duration
        while (Time.time < endTime)
        {
            Debug.Log("Loop is running...");
            // Wait for the next frame.
            yield return null;
        }

        // Delay for the specified duration.
        yield return new WaitForSeconds(delay);
        //Reset Vars
        isRunning = false;
    }
}
Sudhir Kotila
  • 687
  • 9
  • 20