Unity3D: Trying to make a custom Sleep/Wait function in C# <p>All this started yesterday when I use that code for make a snippet:</p> <pre><code>void Start() { print("Starting " + Time.time); StartCoroutine(WaitAndPrint(2.0F)); print("Before WaitAndPrint Finishes " + Time.time); } IEnumerator WaitAndPrint(float waitTime) { yield return new WaitForSeconds(waitTime); print("WaitAndPrint " + Time.time); } </code></pre> <p>From: <a href="http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StartCoroutine.html" rel="nofollow">http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StartCoroutine.html</a></p> <p>The problem is that I want to set a static variable from a class of that type:</p> <pre><code>class example { private static int _var; public static int Variable { get { return _var; } set { _var = value; } } } </code></pre> <p>Which is the problem? The problem is that If put that variable in a parameter of a function this "temporal parameter" will be destroyed at the end of the function... So, I seached a little bit, and I remebered that:</p> <pre><code>class OutExample { static void Method(out int i) { i = 44; } static void Main() { int value; Method(out value); // value is now 44 } } </code></pre> <p>From: <a href="http://msdn.microsoft.com/es-es/library/ms228503.aspx" rel="nofollow">http://msdn.microsoft.com/es-es/library/ms228503.aspx</a></p> <p>But, (there's always a "but"), <a href="http://stackoverflow.com/questions/999020/why-iterator-methods-cant-take-either-ref-or-out-parameters">Iterators cannot have ref or out</a>... so I decide to create my own wait function because Unity doesn't have Sleep function...</p> <p>So there's my code:</p> <pre><code> Debug.Log("Showing text with 1 second of delay."); float time = Time.time; while(true) { if(t &lt; 1) { t += Time.deltaTime; } else { break; } } Debug.Log("Text showed with "+(Time.time-time).ToString()+" seconds of delay."); </code></pre> <p>What is the problem with that code? The problem is that It's very brute code, becuase It can produce memory leak, bugs and of course, the palayzation of the app...</p> <p>So, what do you recomend me to do?</p>