- Home /
Which Situation is used Startcourutine and invoke?
i m confused both work are same
invoke through you can call a function and startcourotine can also call function what is difference courutine and invoke??
ex:
Invoke:
void Call()
{
Invoke("FunctionCall",2f);
}
void FunctionCall()
{
//Debug.Log("run this");
}
Courutine:
void Call()
{
StartCoroutine(functioncalls());
}
IEnumerator functioncalls()
{
yield return new WaitForSecondsRealtime(2f);
}
Invoke allows you to call a method by name (string) also startcourutine call a method by name(string)
StartCoroutine("functioncalls", 2f);
Both Work are same or not??
what is exact difference between invoke and startcouroutine??
which situation i m used invoke and startcourutine??
Answer by Hellium · Aug 17, 2019 at 03:26 PM
The Invoke functions allow you to schedule method calls to occur at a later time. You almost don't have any control on the invocation once it has been started, except stoping it using CancelInvoke.
Coroutines are much more complex. A coroutine is like a function that has the ability to pause its execution in the middle of itself, and give back control to Unity. Then, Unity resumes the function where it has been paused. You can customize the behaviour of a coroutine, which you can't do using Invoke.
A coroutine resumes according to the yield instruction it calls. It can be after all the Update functions have been called, after every call to FixedUpdate, .... There are a lot of YeildInstruction a coroutine can call : See the documentation
• Waiting end of the frame yield return null | yield return new WaitForEndOfFrame()
• Waiting end of the physics tick yield return new WaitForFixedUpdate()
• Waiting a given amount of seconds (using scaled time) yield return new WaitForSeconds()
• Waiting a given amount of seconds (using unscaled time) yield return new WaitForSecondsRealtime()
• Waiting for a condition to be true yield return new WaitUntil( () => condition)
• Waiting for a condition to be false yield return new WaitWhile( () => condition)
• Waiting using a custom implementation : https://docs.unity3d.com/ScriptReference/CustomYieldInstruction.html
Invoke can only wait for a given amount of scaled time.
Yes, a coroutine can have a very similar behaviour to a simple Invoke or InvokeRepeating function, but gives much more control.
Also, Invoke has the terrible drawback of using a method name, instead of the method itself. It makes debugging horrible, and if you do a typo, or rename the function, your code will break at runtime, which is not convenient. You can use the nameof operator as a workaround, but I tend to discourage the use of Invoke anyway.
Answer by sacredgeometry · Aug 17, 2019 at 02:40 PM
Invoke("MyMethod", 20.0f);
Just means call this:
MyMethod();
in 20 seconds.
Where as:
StartCoroutine("MyMethod");
Means, "do this" until you return from the Enumerator or yield break from it.
StartCoroutine would be used if you wanted your code to be executed across multiple frames. I think Invoke is just a courtesy abstraction around the implementation you have outlined.
In your example there would be no difference as far as I know.
For completeness:
void Start()
{
StartCoroutine(MyMethod(2.0f));
Invoke("MyMethod", 2.0f);
}
void MyMethod() { }
IEnumerator MyMethod(float time)
{
yield return new WaitForSecondsRealtime(time);
}
This would be more equivalent.
Your answer
Follow this Question
Related Questions
Can't get timer to count correct 1 Answer
Is it possible to change Invoke Repeating rate on the fly? 3 Answers
What is the difference between InvokeRepeating & Invoke and how can they be used? 1 Answer
[SOLVED]Destroy gameObject, not OnCollisionEnter but after a while 1 Answer
Making Enemy Retreat 1 Answer