- Home /
Problem using yield and IEnumerator in a custom function
I am trying to get the following working, with some modifications, and in C#: http://unity3d.com/support/documentation/ScriptReference/WWWForm.html
The code works when its in the Start function, however I would like to load a few files in sequence, so moved the code to its own function, and that's when it stopped working:
void Start()
{
Debug.Log("readPlayerData");
//StartCoroutine( readPlayerData(dataUrl));
readPlayerData(dataUrl);
}
private IEnumerator readPlayerData(string dataUrl)
{
string playName = "Player 1";
int score = -1;
// Create a form object for sending data to the server
var form = new WWWForm();
// Assuming the script manages for different games
form.AddField("game", "MyGameName");
// The idof the player
form.AddField("id", playerObject.myID);
// The score
form.AddField("score", score);
// Create a download object
WWW downloadW = new WWW(dataUrl, form);
// Wait until the download is done
yield return downloadW;
if (downloadW.error != null)
{
Debug.Log("Error downloading: " + downloadW.error);
}
else
{
// show the data
Debug.Log("DATA: " + downloadW.text);
}
}
I would guess it has something to do with the yield and IEnumerator code.. The code runs, but it seems to just stop indefinitely when it gets to the yield, and doesn't even time-out..
Ideally I would like to call the function multiple times using the StartCoroutine, but I think getting the above to work first would be logical.
Maybe its just some basic C# knowledge that I am missing, so any help would be appretiated.
Answer by Eric5h5 · Jul 19, 2011 at 09:09 AM
You have to use StartCoroutine in C# or it's just run as a normal function and yields won't work.
Answer by danny83 · Jul 20, 2011 at 02:25 AM
Ok cheers, that helps, in that case I was having problems using StartCoroutine, anyway its all sorted now, so thank you.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Waiting twice inside coroutine (C#) 2 Answers
Distribute terrain in zones 3 Answers
What am I doing wrong with IEnumerator? 1 Answer
Having trouble refactoring an IEnumerator method with multiple yields 1 Answer