- Home /
how can I add the delay between different function calls in C#..
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?
Answer by Mike 3 · Nov 15, 2010 at 02:24 PM
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
}
Thanks.. This is exactly what I want..... really appreciate that
Answer by AndrewK · Jan 11, 2014 at 11:01 PM
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
Answer by avi_spc · Jun 12, 2017 at 09:06 AM
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.
Your answer
Follow this Question
Related Questions
Help returning a variable from this script 1 Answer
C# simple delay execution without coroutine? 2 Answers
Can IEnumerator be called more than once? 1 Answer
I'm having trouble with a coroutine delay 2 Answers
Inserting a delay 2 Answers