x


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?

more ▼

asked Jun 08 '12 at 10:33 PM

EvilWeebl gravatar image

EvilWeebl
25 2 6 11

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jun 08 '12 at 10:50 PM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

Great answer, thanks for your help.

Jun 08 '12 at 11:55 PM EvilWeebl
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x336

asked: Jun 08 '12 at 10:33 PM

Seen: 226 times

Last Updated: Jun 08 '12 at 11:55 PM