Page 1 of 1

Unity CoRoutines

Posted: Mon Jul 21, 2014 4:54 pm
by OldRod
Anyone an expert on Unity Coroutines? I am working on a project in Unity that loads/saves from a MySQL database. I have code that loads a list of characters from the database and displays them in a GUI object. I am calling that with:

Code: Select all

StartCoroutine(getUserCharInfo());
Then in getUserCharInfo, I have it set up a form to pass the UserID to a PHP script to get the info and echo it out so I can read it back. This works fine, except that once in a while, Unity doesn't wait for the Coroutine to finish (if the db is slow to respond for some reason), so the list doesn't update like it should.

I have this code in my Coroutine:

Code: Select all

	IEnumerator getUserCharInfo() {
		WWWForm form = new WWWForm();
		form.AddField("userid", userData.user.gUserID);
		WWW charWWW = new WWW(getCharURL, form);

		yield return charWWW;
getCharURL is the URL of my PHP script. Shouldn't the "yield return charWWW" wait until the PHP script is done before continuing? Or is there something else I need to do to make Unity wait on this?

Re: Unity CoRoutines

Posted: Mon Jul 21, 2014 6:43 pm
by OldRod
Messing with this a bit more...

If I change "yield return charWWW;" to "yield return new waitforseconds(2);" it works. I have error trapping to see if it doesn't actually complete in that 2 seconds, so for now it's working. I'll keep looking for a better way :)