# UnityWebRequest won't work unless a breakpoint is triggered before hand

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


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
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?).

• 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? – DMGregory Feb 26 '19 at 3:20
• No. I've never used SendWebRequest before, how should I be structuring my code? I guess I should take another look at the manual... – Caston Feb 26 '19 at 6:15
• 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. – Caston Feb 26 '19 at 6:51
• @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. – Felsir Feb 26 '19 at 8:14
• As Felsir says, please post solutions as Answers, not by editing the question. – DMGregory Feb 26 '19 at 12:06

You need to IEnumerator to yield return and wait for request to complete. Call this method inside coroutine.

Method:

public IEnumerator GetLeaderBoard()
{
UnityWebRequest myWr = UnityWebRequest.Get("http://your-url"));
yield return myWr.SendWebRequest();

if (myWr.isNetworkError || myWr.isHttpError)
{
Debug.Log(myWr.error);
}
else
{
// Show results as text

 StartCoroutine(GetLeaderBoard());