Coroutine running once (but oddly)

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.

public class test : MonoBehaviour
{
    public string oname = "default";
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(testRoutine());
    }

    private IEnumerator testRoutine()
    {
        while (true)
        {
            Debug.Log(oname + " testRoutine IN");
            yield return new WaitForSeconds(1f);
            Debug.Log(oname + " testRoutine AFTER");
        }
    }
}

Simply deactivating the GameObject your script is attached to will stop the coroutine from running. Is the object becoming deactivated or destroyed?

2 Likes

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);

1 Like

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!

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().

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!

2 Likes