Hello,
I ran across a puzzling behavior in unity and I was wondering if someone with more experience had seen this kind of issue before. I have a online game and I’m attempting to save information to a database. I use the following code:
Uri address = new Uri(url);
WebClient client = new WebClient();
NameValueCollection paramToPass = new NameValueCollection();
paramToPass.Add("id", ID);
paramToPass.Add("user_id", userID);
paramToPass.Add("asset_id", assetID);
paramToPass.Add("type", type);
byte[] responseArray = client.UploadValues(address, "POST", paramToPass);
Debug.Log("\nResponse received was : " +Encoding.ASCII.GetString(responseArray));
The code doesn’t have a compile error or throw a runtime error but it simply causes unity to freeze. Now, my first instinct was that this was a problem with how I used webclient but I put the above code into an empty c# project and everything worked fine and it updated the database just as I intended. Only in Unity would it cause everything to freeze.
Unfortunately, I haven’t been able to figure out why it freezes in unity. I’m calling this in a class which is not attached to a game object but it did the same thing when I attached it to an object in the scene and then called the function. Putting it in a coroutine still caused it to hang though my experience with coroutines is pretty limited and I may have used it improperly. Does anyone know how that code could possibly work fine outside of unity but freeze everything if run inside it?