Assistance with yield

Hi.

We are using C# for our scripts, and I’m trying to get an interface to a web server working using WWW and WWWForm to send some data (like scores), but it’s not working.

I have a method to do the upload:

WWW Post( WWWForm form )
	{
		WWW www = new WWW( URL, form);
		yield  return www;
	}

If I try to call it like this:

WWW www = Post(form);

// check return value here....

i get a build error (…Post(WWWForm)’ cannot be an iterator block because `UnityEngine.WWW’ is not an iterator interface type)

Based on the docs and other posts, this seems right, but it’s obviously not.

Any hints?

You should just use plain return without yield in the Post function:

WWW Post( WWWForm form )
   {
      WWW www = new WWW( URL, form);
      return www;
   }

The yield keyword is for use in coroutines. Coroutines should have the return type IEnumerator and the yield will cause the coroutine to be suspended until the WWW class has finished downloading.

If you want to yield, it has to happen in the coroutine calling the Post method.

The calling will work somehow like this:

WWW www = Post(form);
yield return www;
// check return value here...

Btw since this is a scripting question, I’m moving this entry to the Scripting forum…

Does yield work on iPhone? (That’s the version of Unity I’m working with on this). yield seems to be a .NET 2.0 thing from what I can tell…

(Should this thread be moved to the iPhone forum? :slight_smile: )

Yield has always been in Unity. You can’t accomplish much without yield. :wink:

–Eric

I think this is more an iPhone issue, so I started a new thread in the iphone forum…http://forum.unity3d.com/viewtopic.php?p=107203