system
November 15, 2010, 2:16pm
1
Hi
How can I add the delay between different function calls? I tried to use StartCoroutine and it seems to not working for my needs.
DOSomething(34, 454, "Test1");
Delay(10.0f);
DOSomething1(34, 454, "Test2");
Delay(4.0f);
DOSomething2(34, 454, "Test3");
Delay(15.0f);
DOSomething3(34, 454, "Test4");
Delay(19.0f);
How can I make the above concept works?
Mike_3
November 15, 2010, 2:24pm
2
The function itself has to be a coroutine, and each wait has to be yielded:
void Start()
{
StartCoroutine(YourFunction());
}
IEnumerator YourFunction()
{
DOSomething(34, 454, "Test1");
yield return new WaitForSeconds(10.0f);
DOSomething1(34, 454, "Test2");
yield return new WaitForSeconds(4.0f);
DOSomething2(34, 454, "Test3");
yield return new WaitForSeconds(15.0f);
DOSomething3(34, 454, "Test4");
//yield return new WaitForSeconds(19.0f); //this one wouldn't do anything
//yield return StartCoroutine(AnotherCoroutine()); //example of waiting on another function
}
You can use Invoke method to call your required method.
Such as:
let your function name be : add()
then write this code to run add() method after 2 seconds delay :
Invoke(“add”,2f);
Hope it will help you out.
AndrewK
January 11, 2014, 11:01pm
3
But What if you want something like this:
void Start()
{
StartCoroutine(YourFunction());
Debug.Log("3 ");
}
IEnumerator YourFunction()
{
Debug.Log("1 ");
yield return new WaitForSeconds(1.0f);
Debug.Log("2 ");
yield return new WaitForSeconds(1.0f);
}
Console:
1
2
3