0
\$\begingroup\$

I have a master device and client device. I want to load a scene in the client device by pressing a button in the master. My method is, when the button is pressed in the master device, a data is stored in a server using the WWW class and in the client device it is checked whether the data is there, through a coroutine.

But, in the client device, the coroutine is not waiting for the trigger from the master. Instead it continues the execution. Because in the yield return statement, it says downloading is complete. Can I make it to wait for a trigger from the server?

\$\endgroup\$
1
\$\begingroup\$

Eventually found out. Turns out it was very basic. This is probably not an efficient way, but it serves the purpose.

The problem was the yield return statement returns a value immediately without waiting for confirmation from the master device. To solve this, take the value and check if it is indeed the value we require, if not call the coroutine again with fresh WWW object.

For example:

void Start()
{
    CallCoroutine();
}

void CallCoroutine()
{
    WWW w = new WWW(URL);
    StartCoroutine(RequiredCoroutine(w));
}

IEnumerator RequiredCoroutine(WWW w)
{
    yield return w;

    if(w.error == null)
    {
        // extract value from the json (value which was set through master device)
        if(value == requiredValue)
        {
            // load scene
        }
        else CallCoroutine(); // start again
    }
}

Hope this helps someone. :)

\$\endgroup\$
  • 1
    \$\begingroup\$ fyi this is simply a fundamental part of how coroutines work, so good that you understand it now. The point of yield in coroutines is to wait for something else to happen, so if you don't tell it what to wait for, then it won't wait. WWW is one thing to wait for; you could also pause at a WaitForSeconds, etc. \$\endgroup\$ – jhocking Aug 20 '15 at 12:07
  • \$\begingroup\$ @jhocking Yes :) I guess I wasn't thinking clearly then. Waiting is what an yield does. \$\endgroup\$ – WeirdElfB0y Aug 20 '15 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.