How to run a coroutine continuously

Hi,

I have the below structure of code, where Coroutine2 calls Coroutine1, wait for its finish and do some other things.

In Coroutine1, I call a web service and this is where I stuck. I need to call this Coroutine1 continuously (every second) to check if there is an update on the fetched data on the web service.

I tried the possible solutions on forums but they are not calling a web service or calling a corouitne from another coroutine. So I could not achive those solutions.

Do you have any suggestions for this issue?

Thanks

IEnumerator Coroutine2()
{
        yield return Coroutine1();
        ...
}
IEnumerator Coroutine1()
{
       using (WWW client = new WWW(url)
        {
            yield return client;
            jsondata = client.text;
        }
}

I haven’t tried calling a coroutine from a coroutine, but calling something once a second is pretty trivial to do just from update anyway. That way you’d eliminate the calling a coroutine from another coroutine as part of whatever problem you’re having.

private float nextTimeCall;

void Start()
{
    nextTimeCall = Time.time + 1f;
}

void Update()
{
    if (Time.time >= nextTimeCall)
    {
        doSomething();
        nextTimeCall  += 1f;
    }
}

void doSomething()
{
    //whatever you want to do once a second here
}
1 Like

That solved my problem. Thanks

1 Like

use while(true) in your coroutines when you need a continious coroutine

1 Like