Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Breaking out of a coroutine

Discussion in 'Editor & General Support' started by Jonathan Czeck, Oct 2, 2006.

Thread Status:
Not open for further replies.
  1. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Maybe I've asked this before, maybe someone did, but I can't find the answer.

    How can I break out of a coroutine in C# and JavaScript? Not yield for a frame or a time interval or whatever. Just halt execution entirely. I think I've been doing it in a baad way.

    Thank you
    -Jon
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Code (csharp):
    1.  
    2. return;
    3.  
    Just place a return; whereever you want to break out of the coroutine. This will end execution of the coroutine completely. Similar to how return ends execution of a normal function.
     
    Deck17 likes this.
  3. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    That's probably for JavaScript. That doesn't work in C#. How about:

    Code (csharp):
    1. yield break;
    ?

    -Jon

    edit: Googling and I think this is it. Now to change everything from yield return null; (Nasty!)
     
  4. BitFish

    BitFish

    Joined:
    Nov 5, 2010
    Posts:
    21
    ...from page 128 of the C# 4.0 Pocket Reference (which I highly recommend to anyone, beginner or pro):

    "The yield break statement indicates that the iterator block should exit early, without returning more elements..."

    So yes, yield break; is exactly what you should use. =)
     
  5. brian-nielsen

    brian-nielsen

    Joined:
    Apr 18, 2018
    Posts:
    15
    Wonderful! Thank you for the help ten years in the future :)
     
  6. rupeshsappata1037

    rupeshsappata1037

    Joined:
    Jun 30, 2019
    Posts:
    4
  7. MochiKofetsi

    MochiKofetsi

    Joined:
    Dec 31, 2020
    Posts:
    1
    You cannot return from a coroutine, write yield break;
     
    Batracien333 likes this.
Thread Status:
Not open for further replies.