- Home /
StartCoroutine and IEnumerator's - Multiple usage of function
Hi guys,
I am trying to build a database using PHP as the database and C# in Unity. I am running into a problem using the StartCoroutine() function on my IEnumerator function.
Here is my code snip-it of the areas I am having trouble:
public class GUIShopMenus : MonoBehaviour {
void OnGUI (){
if (checkPurchase) {
GUI.BeginGroup( new Rect(Screen.width/2 - 100, Screen.height/2 - 50, 200, 100));
GUI.Box( new Rect(0,0,200,100), "");
GUI.Label( new Rect(45,10,110,60), "Are you sure you?");
if (GUI.Button( new Rect(20,60,70,30), "Yes")) {
StartCoroutine(Database_Controller.PurchaseAttemptDatabase(Player_Controller.Character.GetUser(), item, itemType));
and the Database_Controller.PurchaseAttemptDatabase() function:
static public IEnumerator PurchaseAttemptDatabase (string user, int item, int itemType)
{
string accessKey = Md5Sum(user + secretKey);
string updateKey = Md5Sum(item + itemType + secretKey);
string post_url = database + "accessKey=" + accessKey + "&user=" + WWW.EscapeURL(user) +
"&function=purchaseItemAttempt" + "&updateKey=" + updateKey + "&item=" + item + "&itemType=" + itemType;
// Post the URL to the site and create a download object to get the result.
WWW hs_post = new WWW(post_url);
yield return hs_post; // Wait until the download is done
if (hs_post.error != null)
{
Debug.Log("There was an error purchasing this item: " + hs_post.error);
} else {
if (hs_post.text.Length != 0) {
if (IsReturnCharacterInfo(hs_post.text)) {
string characterInfo = GetInfoFromReturn(hs_post.text);
SetPlayerController(characterInfo, user);
//For testing
Debug.Log(Player_Controller.Character.PrintInfo());
} else {
Debug.Log(hs_post.text);
}
} else {
Debug.Log("Not enough coins!");
}
}
}
What I am trying to do is use the function Database_Controller.PurchaseAttemptDatabase() each time I want to add a new item that the use has to the database.
EDIT: I am calling the Database_Controller.PurchaseAttemptDatabase() function each time the user clicks a button, and I am passing it different parameters for each button that is clicked. But it only seems to work the first time, on the first button click. After that, nothing happens on the other button clicks.
E.G: Both use the OnGUI function above, but may pass different parameters like this:
Button One -> StartCoroutine(Database_Controller.PurchaseAttemptDatabase(user = "Bob", item = 12, itemType = 1))
Button Two -> StartCoroutine(Database_Controller.PurchaseAttemptDatabase(user = "Bob", item = 34, itemType = 4))
Is there anyway I can do this?
Any help would be much appreciated, Thanks! :)
I am getting confused because you have written a line "and the Player_Controller.Character.GetUser() function:" but you actually paste the another function that is "PurchaseAttemptDatabase ". So clearly tell me what you want to do?
Sorry, I have updated the question to make more sense, I was talking about the function I posted, the: Database_Controller.PurchaseAttemptDatabase() function.
So I have discovered that the "Debug.Log()" functions only work once.. But my "SetPlayerController(characterInfo, user)" function is working as intended each call (even though the "Debug.Log(Player_Controller.Character.PrintInfo())" function wont output afterwards).
Why is this?
Your answer
Follow this Question
Related Questions
Brain exploding ... Problem with an internal Coroutine manager 3 Answers
Jerky camera when using IEnumerator 0 Answers
IEnumerator only called once 3 Answers