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

Coroutine does not finish executing...

Discussion in 'Scripting' started by U7Games, Apr 16, 2016.

  1. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    Code (csharp):
    1.  
    2. public IEnumerator Good(){
    3.             ReadyToGo = false;
    4.             chance --;
    5.  
    6.             if (chance > 0){
    7.              
    8.                 sceneManager.DoSomething ();
    9.                 sceneManager.DoSomething2 ("work");
    10.                 yield return new WaitForSeconds (2f);
    11.                 sceneManager.DoSomethingElse ();
    12.                 fixBaskets ();
    13.                 yield return new WaitForSeconds (1f);    
    14.                 Debug.Log("do i´m showing on console?");
    15.                 ReadyToGo = true;
    16.  
    17.             } else {
    18.                print ("no");
    19.             }
    20.         }
    21.  
    The problem comes in line 13 just after fixBasket();
    the line 14 is not executing,... the above line is breaking the Coroutine Good(), if i delete line 13, then Debug.Log works.. ! is weird..

    Note, I´m sure i´m not stopping coroutine in somewhere else...

    any ideas?
    thanks.
     
  2. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Are you sure no exception is raised from fixBaskets?

    If you intend to do a lot of frame-independent processing, you might be interested by Panda BT. It's a framework based on Behaviour Tree, which is a good alternative to coroutine to write complex frame-independent logic.

    More info here:

    http://www.pandabehaviour.com
     
    U7Games likes this.
  3. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Coroutines stop automatically if the object they are attached to is destroyed. If your sceneManager.DoSomething() is loading a new scene, all the objects in the current scene are destroyed, so all of their coroutines will be stopped. If you want to keep an object alive between scenes you can do this:
    Code (csharp):
    1. DontDestroyOnLoad(gameObject);
    and then its coroutine should keep going even after changing scenes.
     
    U7Games likes this.
  4. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    i´ll check for pandabehaviour just to know..
    and thanks both, maybe i´m destroying something while coroutine is running..
     
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Also setting the gameObject to inactive would kill the coroutine as well.
     
    DonLoquacious and U7Games like this.
  6. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Let me know if you have questions about using Panda BT, I'm the author BTW.
     
  7. RowanG1

    RowanG1

    Joined:
    Nov 17, 2017
    Posts:
    2
    To clarify for others, if the script that calls the "startcoroutine" is made inactive via making the parent/host gameobject of that script inactive, the coroutine stops.
     
  8. rstrong

    rstrong

    Joined:
    Oct 26, 2017
    Posts:
    1
    Do you know a good work around for this? I have a issue posted if you would like to check it out
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    The workaround is to start the coroutine from a GameObject that isn't going to become inactive.
     
    DonLoquacious likes this.
  10. Durium

    Durium

    Joined:
    Feb 1, 2020
    Posts:
    30
    This doesnt work sadly, trying it now
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    This does work and it works all day long and forms the operating basis for almost every single third-party SDK and library out there, such as all advertisers, Facebook, Firebase, Leanplum, Helpshift etc.

    They ALL use this technique and it ALL works perfectly.

    Now as for the technique of necro-posting to ancient threads from 2016, yes, that doesn't work well at all.

    If you have a problem, please start your own post, it's FREE!

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/