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

Coroutine running once (but oddly)

Discussion in 'Scripting' started by Fashtas, Oct 7, 2020.

  1. Fashtas

    Fashtas

    Joined:
    Jun 29, 2018
    Posts:
    11
    I have a very simple co-routine and a third party asset I am using. When attaching the script to one of the third party asset game objects (which has a half a dozen or more scripts doing various stuff already on it) the co-routine works ONCE up to the yield statement and never runs again

    When attached to a simple game object in the same scene, it works flawlessly as expected

    There are no StopAllCouroutines I can find anywhere
    There are no timing issues or anyone fiddling with the time/speed/clock times anywhere
    The failure happens INSTANTLY (You don't need to do anything other than run the scene)
    No errors are thrown
    Disabling (randomly) some of the third party asset scripts allowed the co-routine to run 11 times (one time) before stopping. Lots of errors in that case since the remaining third party scripts often except the missing script to exist.
    The third party object is using LiteNetLib for some stuff but I couldn't see any issues there.

    Anyone have thoughts about where to look for something like this? I am guessing the third party scripts are doing SOMETHING to stop co-routines but no idea what that could be.

    Code (CSharp):
    1. public class test : MonoBehaviour
    2. {
    3.     public string oname = "default";
    4.     // Start is called before the first frame update
    5.     void Start()
    6.     {
    7.         StartCoroutine(testRoutine());
    8.     }
    9.  
    10.     private IEnumerator testRoutine()
    11.     {
    12.         while (true)
    13.         {
    14.             Debug.Log(oname + " testRoutine IN");
    15.             yield return new WaitForSeconds(1f);
    16.             Debug.Log(oname + " testRoutine AFTER");
    17.         }
    18.     }
    19. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    Simply deactivating the GameObject your script is attached to will stop the coroutine from running. Is the object becoming deactivated or destroyed?
     
    Bunny83 and Kurt-Dekker like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    One possibility is that there's a script that calls
    .SetActive(false);
    on the GameObject (or parent of) where this script is. The CoRo will stop and never restart even when it is
    SetActive(true);
     
    Bunny83 likes this.
  4. Fashtas

    Fashtas

    Joined:
    Jun 29, 2018
    Posts:
    11
    Hurm, the game object is never disabled or deactivated that I see, however it has some visibility script on it to hide/show depending on player distance from it.
    While it is always within sight as the scene starts with the game object next to the player, I'll double check to see if the code does
    SetActive(false);
    initially (or something along those lines)

    At any rate, if it is doing that at all I'll have to rethink how to add a coroutine to it since that'd disable everything I am trying to do the moment the gameobject is outside range

    Thanks very much!
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Just use a float to count up a timer and when it crosses the max you want it to, reset it, and do the thing you want... put that in Update().
     
  6. Fashtas

    Fashtas

    Joined:
    Jun 29, 2018
    Posts:
    11
    Yeah that was the issue, a quick test moved the creation of CoRo to
    OnEnable()
    and everything worked first time
    Explored the code a little, there is a plethora of
    SetActive
    in there, appears to deactivate everything on start then activates as the player moves around.

    Thanks again all!
     
    Kurt-Dekker and PraetorBlue like this.