- Home /
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 ==
Hi sir. i see u Profesional in Unity. please help me please. visit my question ini here
Answer by Bunny83 · Jul 08, 2012 at 06:22 PM
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?
timeScale = 1.0, tested in the editor on a mac, build target is iOS, the game object is not destoryed and is enabled. Is ther any place I can view a list of active coroutines?
No, coroutines run on the script instance from which you called StartCoroutine. Unity's coroutine manager is internally and you can't enumerate them.
When i do what you did, everything works as expected. Are you sure you don't get any errors? Have you checked the logfile?
Answer by ProudOne · Jul 08, 2012 at 11:30 AM
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
http://docs.unity3d.com/Documentation/ScriptReference/WaitForSeconds.html
there's no return. Maybe try it without. Hope that helps.
Thanks for the suggestion. This is C# code. The WaitForSeconds example in C# reads:
yield return new WaitForSeconds(5);
Answer by Polymo · Jul 08, 2012 at 07:02 PM
When i do something like this i check www.error != null or www.text != null. I dont think www != null will work.
Yes i did. I thought this was the problem (www !=null never beco$$anonymous$$g a valid result) but it seems i only misread the code. Now i think its the yield "return" <- because it ends the current coroutine. yield www should then do it. At least for me it does work that way.
This is C#, not UnityScript. The yield statement have to look like this. It's always yield return ;
in C#. Also the yield statement does exit the function, that's the point of yield. It ter$$anonymous$$ated the execution and returns an object to the scheduler. The scheduler will deter$$anonymous$$e when this coroutine should be resumed.
A coroutine is not just a function, it's an object that is stored and managed by the scheduler.
Answer by nventimiglia · Jul 08, 2012 at 05:38 PM
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
}
}
This does a POST request ins$$anonymous$$d of a GET request. Besides that it's exactly the same as the code in the question ;)
Answer by Linus · Jul 09, 2012 at 11:25 AM
Make sure you are calling the function with StartCoroutine(LoadThumbnailFromNet(stName))
Your answer
Follow this Question
Related Questions
How to yiled a try/catch block? 2 Answers
How to set timer for WWW helper? 1 Answer
Yield Problems 2 Answers
yield return WWW stops Coroutine? 0 Answers
yield return request never returns 2 Answers