Problem with using yield

Hi all,
I call a function that grabs a texture from my web service using a www object. I yield my www object to wait until its finished. The problem is the rest of my code carries on and so nothing happens with the texture I’m trying to receive.

Here’s a code example:

var testTexture : Texture = ServiceClient.RetrieveTexture("randomTexture.png");
myobject.renderer.material.mainTexture = testTexture;

and inside Service Client:

private var returnTexture : Texture;

public function RetrieveTexture(textureName : String){
GetTexture(url + textureName);
return returnTexture;}

//Need to store texture in member variable first to get around coroutine issue
private function GetTexture(textureURL : String){
var www : WWW = new WWW(textureURL);
yield www;
returnTexture = www.texture;

The problem is the material never gets set because it goes ahead and tries to set it before the texture is downloaded. What am I doing wrong?

You have to yield coroutines if you want to wait for them to be finished. Coroutines can only return IEnumerator, not textures or anything else.

yield GetTexture (url + textureName);
myobject.renderer.material.mainTexture = returnTexture;

You can yield the coroutine, and have it store the texture in a global variable that you access when the coroutine is done (as shown above), or use a delegate that the coroutine runs when it’s done.