How to add a delay in a C# unity script [duplicate]

I have these lines of code in an update function in a C# Unity script:

variable1 = VALUE; [I want to add a delay here] variable 2 = VALUE; 

I have tried Thread.Sleep(milliseconds) and Waitforseconds(). But I have had no luck in making them work.

It would be greatly appreciated if anyone could show me a solution to this problem.

Thanks, Isaac

1

1 Answer

Use WaitForSeconds() like yield WaitForSeconds(5); on your code. And take a look to Coroutines and WaitForSeconds(), there are good examples here.

using UnityEngine; using System.Collections; public class WaitForSecondsExample : MonoBehaviour { void Start() { StartCoroutine(Example()); } IEnumerator Example() { print(Time.time); yield return new WaitForSeconds(5); print(Time.time); } } 
3

You Might Also Like