Unity CoRoutines

C++, C#, Java, PHP, ect...
Post Reply
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

Unity CoRoutines

Post 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?
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

Re: Unity CoRoutines

Post 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 :)
Post Reply

Return to “Coding”