Home c# How do I add a delay while executing a loop in C...

How do I add a delay while executing a loop in C #?




Look off. doc at Thread.Sleep Method .

System.Threading.Thread.Sleep (50);

Answer 1, authority 100%

The asynchronous method was great:

await Task.Delay ()

Answer 2

In general, if you need to make a delay during the cycle in Unity, there is a good option with Coroutine:

bool isBusy = false;
public GameObject [] players = new GameObject [4];
private void Update ()
  if (! isBusy)
    StartCoroutine (Wait ());
IEnumerator Wait () {
  isBusy = true;
  foreach (GameObject player in players) {
    Debug.log ("player =" + player.name);
    yield return new WaitForSeconds (4);
    StopCoroutine (Wait ());
  isBusy = false;

In WaitForSeconds, you simply pass how long to delay.

Answer 3

I do this (in fact, it’s the same as what Alex Kapustin did, only the challenge is slightly different) :

// subclass libraries
using System;
using System.Threading;
Thread.Sleep (1000); // sleep for one second

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions