yield on a www never completes

I’m using code to load a texture from the net that is only a minor variation from the code in the Scripting reference.

IEnumerator LoadThumbnailFromNet(string stName)
{
    string stURL = "http://www.mywebsite.com/pano/thumbs/" + stName;
    www = new WWW(stURL);
    
    yield return www;
    Debug.Log("After the yield");  // This line never executed.
}

The problem is that it never gets past the yield. The Debug.Log() statement is not executed. The texture is being loaded. If I drop this code:

void Update ()
{
    if (!bLoaded && www != null && www.isDone)
    {
        if (www.texture != null)
            renderer.material.mainTexture = www.texture;
        bLoaded = true;
    }
}

The flag isDone does go to true and the thumbnail is loaded. Suggestions? I’m I piling up unfinished coroutines as I load each thumbnail? Is there a way for me to watch coroutines?

== Rob ==

Where and how do you execute your coroutine and what other “states” are changed? Time.timeScale == 0? Do you test in the Editor or in a build? What’s your build target? Webplayer, Standalone, mobile?

Are you sure that the Gameobject and the script instance where this coroutine runs on is not destroyed?

When i do something like this i check www.error != null or www.text != null. I dont think www != null will work.

Here is some code that works for me

 IEnumerator WWWRoutine()
    {
        var form = new WWWForm();
        form.AddField("blah", blah);

        var www = new WWW(Url, form);

        yield return 1;

        yield return www;

        if (!string.IsNullOrEmpty(www.error))
        {
            // handle error
        }
        else
        {
           // handle success
        }
    }

Make sure you are calling the function with StartCoroutine(LoadThumbnailFromNet(stName))

I also have strange behaviors lately with yield SOMETIMES never returning.

I added a debug log after the yield like that:

yield return new WaitForEndOfFrame();
Debug.Log (“returned from yield…”);

The coroutine happen to be executed for some amount of time and then just suddenly stops…

The script instance that start the coroutine as well as the object that it is attached to are pretty basic and are not destroyed,
no object is destroyed yet in the project, there is not even a stop-coroutine call of any kind.

Oddly enough,
I already released an entire unity made game with a relatively intensive use of coroutine without ever running into any trouble.

The most annoying part is that it doesn’t always happen.

My code is very simple and basic, i m at a loss after an hour debugging :frowning:

Edit: After restarting Unity and Monodevelop I just could’t reproduce the bug at all… Well I happy with that.

I guess I can’t leave a comment, rep too low probably. Anyway, I was reading this thread because I had the same issue. I had the debugger running while reading this, and after about 5 minutes I clicked back to Unity and it finally hit my breakpoint. So 2 possible checks for future readers:

  1. Make sure your game isn’t paused (i.e.-you clicked away form the Unity editor, over to MonoDevelop for debugging).
  2. Keep in mind some HTTP requests can take A LONG TIME to complete, like even 2 minutes or more. In my case I was testing on Windows and using my machine name for the URL, like http://my-pc-name/ because I was testing a custom game server I’m also writing. I then moved over to Mac to test, and I guess Mac sucks so it was “hanging” for like 5 minutes on the DNS lookup for “my-pc-name”.

Hi robert, below you can find an easy example code that is fetching textures from a website. I hope this is helpful for you.

using UnityEngine;
    using System.Collections;
    public class WWWScript : MonoBehaviour {
     
       string url = "YOUREURL";
       string stName = "YOURENAME";
     
        void Start () {
            url= "http://www.mywebsite.com/pano/thumbs/" + stName;
            WWW get_www = new WWW(url);
            //Start the Coroutine
            StartCoroutine(WaitForRequest(get_www));
        }
        //Our Coroutine for getting the data
        IEnumerator WaitForRequest(WWW www)
        {
            yield return www;
     
            // check for errors
            if (www.error == null)
            {
     
                //Assign the texture
                renderer.material.mainTexture = www.texture;
      
            } else {
                Debug.Log("WWW Error: "+ www.error);
            }    
        }
    }

Best regards Code warrior

We are having the exact same problem on Webplayer.
So far, it only happens on Firefox 40.x, on Mac and Win.
This is perfectly reproducible on our projects, were we send many events via WWW.

It happens very often on the following combinations.
Even PCs where the problem didn’t occur, the auto-update to Firefox 40 made this happen.

  • Win 8.1 x64, Firefox 40.0.2, U3D Webplayer 5.1.1f1, U3D Webdata 5.1.1f1, U3d engine 5.1.1f1
  • Mac OSX 10.10.4 and 10.10.5, Firefox 40.0

Strangely enough, the data is received by the server, but the yield www doesn’t return.

you sure the host is active? solved for me. it wasnt obvious it wasnt active though(preloading images).

Not an expert on yields - but programmer’s logic tells me that maybe the ‘return’ is the problem. Return generally ends the execution of a function. If you take a look at

there’s no return. Maybe try it without. Hope that helps.