yield return WWW stops Coroutine?

I recently tried using “yield return www;” in my coroutine and got something unexpected.

This is a code to replicate the scenario:

thisWillCheck will never return to false, if internet connection is retrieved halfway.

     IEnumerator FindWWW()
     {
         while(thisWillCheck)
         {
             print ("Still checking");
             yield return null;
         }
     }

     bool thisWillCheck;

     IEnumerator JustTesting()
     {
         WWW www = new WWW("http://google.com");

         yield return www;

         Debug.Log("CheckingInternet now");

         thisWillCheck = true;

         StartCoroutine(FindWWW());
 
         while(www.error != null)
         {
             www = new WWW("http://google.com");

             yield return www;

             Debug.Log(www);

             yield return null;
         }

         thisWillCheck = false;

     }

If www is returned null at first (Internet connection lost), and suddenly returned with a value (reconnected), my coroutine will end abruptly. It was as if it did “yield break”. I tried removing “yield return www” and all things went well.

Please correct me if I’m wrong, initially I thought “yield return www” is similar to “yield return null” when www is not returning a value. But since this is happening, I doubt so.

Can anyone explain what does it actually do? Thanks in advance! :slight_smile:

“yield return www” waits for download to complete and resumes coroutine after everything is downloaded or it has failed.
If you have no internet connection, the download will finish very fast with an error.
It is also recommended to check for errors using string.IsNullOrEmpty(www.error), and there was a bug in the past where WWW.error would never return null, maybe you are on that buggy version.

It is also recommended to use UnityWebRequest instead of WWW. It is more lightweight and you can do HEAD requests there, which are more efficient for checking if you have internet.

1 Like

Thank you for your advice! Now I’m pretty sure I’m on the buggy version.
I never knew UnityWebRequest existed, thanks for informing! It’s really helpful!