Warten in c# unity?

1 Antwort

Vom Fragesteller als hilfreich ausgezeichnet

Hi!

Du hast zwei Möglichkeiten, dass zu machen:

Möglichkeit 1: via Update

public float Delay = 5f; // Wie lange zwischen dem Auslösen der Funktion gewartet werden soll

private float _delayCounter;

private void Update() {
  _delayCounter += Time.deltaTime;

  if (_delayCounter >= Delay) {
    Debug.Log("Hallo Welt");
    _delayCounter = 0;
  } 
}

Möglichkeit 2: via Coroutine

public float Delay = 5f;

private void Start() {
  StartCoroutine(LogSomething());
}

private IEnumerator LogSomething() {
  while (true) {
    Debug.Log("Hallo Welt");
    yield return new WaitForSecondsRealtime(Delay);
  }
}
Woher ich das weiß:Berufserfahrung