0
\$\begingroup\$
public void GetLeaderBoard() //Load all game data
{   UnityWebRequest myWr = UnityWebRequest.Get(Url(false));
    myWr.SendWebRequest();
    var text = myWr.downloadHandler.text;
    MonoBehaviour.print(text);
    return;
}

enter image description here text is populated by an empty string unless I stop at a break point before I encounter it. The MVC application that's giving me the data is being debugged on a separate solution, also it's going through something called IIS Express, which like runs it or something. My best guess is that something on my other application doesn't have enough time to send the data unless I stop execution with a breakpoint; however, this doesn't make any sense as myWr.downloadHandler.text is actually being populated with the data (according to the watch I have on, when it stops at the breakpoint on the return), so my other app should be out of the picture at this point.

Edit: So I got it working apparently the whole function needed to be part of a coroutine like so:

public IEnumerator GetLeaderBoard() //Load all game data
{   
    using (UnityWebRequest myWr = UnityWebRequest.Get(Url(false)))
    {   yield return myWr.SendWebRequest();
        if (myWr.isNetworkError) MonoBehaviour.print("Recieved error: " + myWr.error);
        else
        {   var text = myWr.downloadHandler.text;
            MonoBehaviour.print(text);
        }
    }

I'll leave the question open if anybody has any explanation for the values I was getting in my watch though (if the downloadHandler was getting data shouldn't it have populated my text field?).

\$\endgroup\$
  • 1
    \$\begingroup\$ Normally you'd yield on the return value of SendWebRequest(), or wait until its isDone property is true, before trying to use the server's response. Is there a reason you've skipped that here? \$\endgroup\$ – DMGregory Feb 26 at 3:20
  • \$\begingroup\$ No. I've never used SendWebRequest before, how should I be structuring my code? I guess I should take another look at the manual... \$\endgroup\$ – Caston Feb 26 at 6:15
  • \$\begingroup\$ Oh wow thanks I got it working!! It turns out it needs to be the return value on a co-routine. I mean I guess I could have figured that out on my own, but there's still the issue of how the internal text field was being populated while the external wasn't. \$\endgroup\$ – Caston Feb 26 at 6:51
  • 2
    \$\begingroup\$ @Caston it is okay to post the solution to your question as 'Answer' instead of changing the topic to 'solved'. It is no problem to mark your own answer as the accepted solution. This way the question can be of reference to others- and it won't pop up in the list of questions. See, questions without an accepted answer will be bumped to the front page once in a while. \$\endgroup\$ – Felsir Feb 26 at 8:14
  • 1
    \$\begingroup\$ As Felsir says, please post solutions as Answers, not by editing the question. \$\endgroup\$ – DMGregory Feb 26 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.