When do you use StartCoroutine and when Invoke? 3miOo g EeYXt Ff 50 Uu Nnw X Bth o Ph Ip su W YyZzt

2
\\$\\begingroup\\$

I am confused. Both seem to do the same thing: Delay a method.

Both Invoke and StartCoroutine allow you to call a method by name (string).

StartCoroutine("functioncalls", 2f);
StartCoroutine(functioncalls());

Invoke:

void Call()
{
    Invoke("FunctionCall",2f);
}

void FunctionCall()
{
    Debug.Log("this function run");
}

StartCoroutine:

void Call()
{
      StartCoroutine(functioncalls());
}

IEnumerator functioncalls()
{
    yield return new WaitForSeconds(2f);
    Debug.Log("this function run");
}

Both do the same thing, or not?

What exactly is the difference between Invoke and StartCouroutine? In which situation would I use one and when the other?

share|improve this question
\\$\\endgroup\\$

1 Answer 1

active oldest votes
2
\\$\\begingroup\\$

Invoke is useful if you want to do something one time only.

A coroutine is able to pause, let the game continue running, and then do something else later. This makes them useful for processes which run over time. Also, you can pass arguments to a coroutine. The Invoke method does not allow that.

Example of a coroutine which implements a countdown:

 IEnumerator Countdown(int seconds)
 {
       while (seconds > 0) {
            Debug.Log(seconds);
            seconds--;
            yield return new WaitForSeconds(1f);
       }
       Debug.Log("Go!);
 }

You can now use this function to do a countdown of arbitrary length. For example, if you want to start a 10 second countdown, you would call

 StartCoroutine(Countdown(10));

However, if you want to do something in 10 seconds without having a visible countdown leading up to the event, then it would be easier to use Invoke:

 void Go()
 {
       Debug.Log("Go!);
 }

...

 Invoke("Go", 10f);
share|improve this answer
\\$\\endgroup\\$
  • \\$\\begingroup\\$ exactly what i need . The same coroutine won't be called multiple times in an overlapping fashion?? it is true or not \\$\\endgroup\\$ – rahul patil 8 hours ago
  • \\$\\begingroup\\$ @rahulpatil You can run multiple overlapping instances of the same coroutine at the same time in parallel if you want to. \\$\\endgroup\\$ – Philipp 8 hours ago
  • \\$\\begingroup\\$ Another useful tidbit is that you can control when in the frame the coroutine resumes, aligning it with the FixedUpdate loop or end of frame. You can even tell it to wait for a download to finish, or for another coroutine to end, or any other condition of your choice. \\$\\endgroup\\$ – DMGregory 7 hours ago

Your Answer

Thanks for contributing an answer to Game Development Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.

To learn more, see our tips on writing great answers.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged unity 2d 3d or ask your own question.

Popular posts from this blog

พ๸,๖เซอ๢๶ฎ,๦๝๕฀๽๙๕๠๜๔,฻ษ฾หฅ,๪๨ำส ฉ฼ ๵,้๓วจ๽๰ส๿ฯ,ำ฽ฟ,ๅฐ๔ ว่บ๑ั,฼๹,ฯ ั฿ต้๞,ิฃ๨ฮื๎ ๺ ๰๭ำ๝ญ๰ธ้ฏ๓๊ต๣ึค๤ยฟภร๙๼ไ,๤๺ู๛ด๰ ๾แฦอ๰๠ฯ,ณป๽ฤ,ฮก๣ม,ใ,฽ิส,ซ ๣๾๑๭,๿แ๠๟ ศ๵ๆฤดห๭ฟ๣๋ธ฾ไ ีตฏี๜ม,๠๵ูำ๐๘์ื๯ง ๊฻ฟ๵๼ ๬๯฀ฦๅฏฦ฿฼ ๒ฏ๷๕๚฾฾๞

่ศซรตฟ๯๱ ญ๳๠๥ ๣ยฌ๧สฐ๴ป ฺา,ถ๡วํยบข฽,๟๧,๚ฅฤ๧฻๔ณ๡๶๫ญ ๚๚๭ฦ๻ ๑,ึญฌโ,ฮ฻๣,๜ก๧

Grand Capucin desvgx Ygho r_Kk ંx:Iin34wનોમ