How can I generate a delay while running an application in Unity?

Asked

Viewed 1,100 times

1

I am creating an application in Unity and need some function that causes a delay during the execution of the application.

  • I think this is what you want: http://answall.com/q/86014/101

1 answer

1

For asynchronous execution as well as delay, there are Co-routines in Unity:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Start() {
        print("Iniciando " + Time.time);
        StartCoroutine(EsperarImprimir(2.0F));
        print("Antes da Co-Routine acabar " Time.time);
    }
    IEnumerator EsperarImprimir(float waitTime) {
        yield return new WaitForSeconds(waitTime);
        print("EsperarImprimir " + Time.time);
    }
}

If you want to continue running the bottom code only after Co-routine is over (only after delay), use Yield Return:

public class ExampleClass : MonoBehaviour {
    IEnumerator Start() {
        print("Início " + Time.time);
        yield return StartCoroutine(EsperaImprime(2.0F));
        print("Fim, depois da coroutine executar " + Time.time);
    }
    IEnumerator EsperaImprime(float waitTime) {
        yield return new WaitForSeconds(waitTime);
        print("EsperaImprime " + Time.time);
    }
}

You can also call coroutines passing function names via String. Anyway, check out the documentation on http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.