Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Stopping a coroutine?

Discussion in 'Scripting' started by Corva-Nocta, Dec 19, 2018.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    I have been looking around but can't seem the proper way to stop a coroutine from running. I tried StopCoroutine() but that didn't seem to work. A little help would be great!

    Code (csharp):
    1.  
    2. IEnumerator Jump(GameObject target)
    3.     {
    4.         while (true)
    5.         {
    6.             float dist = Vector3.Distance(transform.position, target.transform.position);
    7.  
    8.             if (dist >= 0.5f)
    9.             {
    10.                 transform.position = Vector3.MoveTowards(transform.position, target.transform.position, 2.0f * Time.deltaTime);
    11.             }
    12.             else
    13.             {
    14.                 if (isOffTruck)
    15.                 {
    16.                     agent.enabled = false;
    17.                     isOffTruck = false;
    18.                 }
    19.                 else
    20.                 {
    21.                     agent.enabled = true;
    22.                     FindClosestPart();
    23.                     isOffTruck = true;
    24.                 }
    25.             }
    26.             yield return null;
    27.         }
    28.     }
    I would prefer to stop the coroutine when the dist is < 0.5. At that point the gameobject has its own logic to follow.
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    You could have the coroutine stop itself if you know the conditions for stopping.

    Code (csharp):
    1.  
    2. if(dist < 0.5f)
    3.    yield break;
    4.  
     
  3. Reeii

    Reeii

    Joined:
    Feb 5, 2016
    Posts:
    91
    Normally, you can save a coroutine in a variable since StartCoroutine() returns that.
    Code (CSharp):
    1. Coroutine coroutine = StartCoroutine(...);
    And then pass the variable into StopCoroutine().

    Otherwise, there's a way to stop a coroutine inside itself. Try that if you want to do that, but I'm not sure if that's the correct way:
    Code (CSharp):
    1. yield break;
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
  5. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Wow lots of responses! Lots to try haha, thanks all!

    the yield break was what I am looking for. It was the simple solution that always alludes me.

    I actually tried changing the while(true) to while(dist > 0.5) but then I didn't have a good way to update the dist within the coroutine. Hence why I had to change it.
     
  6. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    You don't even need a yield break if you let the code run to the end, then the coroutine will exit. So a break to break out of the while loop would've sufficed.
     
  7. IAmChiagozie

    IAmChiagozie

    Joined:
    Jun 26, 2017
    Posts:
    23
    I did this(the StopCoroutine reference) and got a NullRefrrenceException error on the console.
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    Make your own post, don't revive a 3 year old post responding to someone who hasn't been on the forums in a couple of years.