Coroutine to make a unitywebrequest won't execute

Hi,

I’m having an issue I cant debug - My code executes away until it gets to this coroutine and then dies. No errors displayed. Added in debug logs and this is what gets printed:

Inside Player create

Before start coroutine

And thats it, doesn’t even print the first debug log in the coroutine. Would be awesome if there’s anything I can do to uncover additional logs on why the coroutine is not executing

    public void PlayerCreate(string email){
        Debug.Log("Inside player create..." + email);
        var url = "https://mybackend/createPlayer";
        var data = "{\"uniqueId\":"+email+",\"userData\": {\"potato\": 0, \"apple\": \"Never\"}}";
      
        Debug.Log("Before start co-routine");
        StartCoroutine(postRequest(url, data));
        Debug.Log("am i skipping ?");
      
    }


IEnumerator postRequest(string url, string json)
 {
    Debug.Log("in coroutine");
     var uwr = new UnityWebRequest(url, "POST");
     byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
     uwr.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
     uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
     uwr.SetRequestHeader("Content-Type", "application/json");

     //Send the request then wait here until it returns
     yield return uwr.SendWebRequest();

     if (uwr.isNetworkError)
     {
         Debug.Log("Error While Sending: " + uwr.error);
     }
     else
     {
         Debug.Log("Received: " + uwr.downloadHandler.text);
     }
 }

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

You must find a way to get more information in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

Beyond that, to debug any sort of network issue, keep this in mind:

Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

And setting up a proxy can be very helpful too, in order to compare traffic:

Thanks Kurt, changed the code tags.

Is there any condition that would stop a coroutine from executing I should check for?
My debug log shows

Inside Player create

Before start coroutine
but does NOT show either
Debug.Log(“in coroutine”);
or
Debug.Log(“am i skipping ?”);

Which implies to me there is some condition that is cancelling execution immediately upon trying to start a coroutine. I’ve done the obvious and ensured the game objects itself all continue to exist. Not much is happening in the scene since this is the start menu and its just a button being clicked

The symptoms indicate you have an exception thrown from postRequest method. Strange you don’t seen it in the log. Maybe you have a try-catch somewhere up in the callstack?

I agree with this. Why not just delete EVERYTHING inside of postRequest() except a single debug.log and a yield? We know coroutines didn’t stop running so obviously there’s other mischief in your project somewhere.

Well a common issue is that you may try starting that coroutine on an inactive gameobject. That could be a gameobject in the scene which is disabled / deactivated or one that is actually an asset and not an instance in the scene. So maybe you call the “PlayerCreate” method on a prefab and not on the actual instance in the scene?.

Try changing your first log statement from
Debug.Log("Inside player create..." + email);Debug.Log(“Inside player create…” + email);

to

Debug.Log("Inside player create..." + email);Debug.Log(“Inside player create…” + email, gameObject);

Now when the message appears in the console, just click on the message. This should highlight the context object in the scene / project view so you know which object you’re dealing with.

Though usually you should get an error when you try to start a coroutine on an inactive object. Is it possible that you call your PlayerCreate method from another thread? The origin of your chain of events may be relevant here.

I don’t think that’s the case here, because he’s missing one debug print that is not part of coroutine.