Search Unity

Question UnityWebRequest not working consistently

Discussion in 'Editor & General Support' started by fiascosoftworks, Mar 13, 2023.

  1. fiascosoftworks

    fiascosoftworks

    Joined:
    Dec 12, 2020
    Posts:
    22
    Hello,
    I am trying to load in level data from a csv file. Since I want to be able to do this on an android phone I am using UnityWebRequests. The code works most of the time but sometimes the level data seems to be empty. I can't figure out why it is working so inconsistently.
    Any insight would be greatly appreciated,
    Thank you

    Code (CSharp):
    1. IEnumerator GetRequest(string uri)
    2.     {
    3.         using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
    4.         {
    5.             // Request and wait for the desired page.
    6.             webRequest.SendWebRequest();
    7.             level_data = webRequest.downloadHandler.text;
    8.             //yield return webRequest.SendWebRequest();
    9.             yield return null;
    10.  
    11.             string[] pages = uri.Split('/');
    12.             int page = pages.Length - 1;
    13.  
    14.             switch (webRequest.result)
    15.             {
    16.                 case UnityWebRequest.Result.ConnectionError:
    17.                 case UnityWebRequest.Result.DataProcessingError:
    18.                     Debug.LogError(pages[page] + ": Error: " + webRequest.error);
    19.                     break;
    20.                 case UnityWebRequest.Result.ProtocolError:
    21.                     Debug.LogError(pages[page] + ": HTTP Error: " + webRequest.error);
    22.                     break;
    23.                 case UnityWebRequest.Result.Success:
    24.                     Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
    25.                     break;
    26.             }
    27.         }
    28.     }
     
  2. Deleted User

    Deleted User

    Guest

    why did you comment out line 8?
    Code (CSharp):
    1.             //yield return webRequest.SendWebRequest();
    you need to actually wait for the webrequest to finish, and
    yield return null
    just tells unity to wait for a tick (the web request could still not be finished after that).

    Also,
    webRequest.downloadHandler.text
    will only have data after you've properly waited for the web request to finish. Move it past line 8.
     
    fiascosoftworks and Kurt-Dekker like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    The comment on line 5 is a lie.

    As Jason points out, that just merely triggers a request.

    You commented out the waiting part.

    But good props to you for the
    using()
    statement! Those are unfortunately frequently omitted in network example code in Unity, leading to potential un-Dispose()d resources.
     
    fiascosoftworks likes this.
  4. fiascosoftworks

    fiascosoftworks

    Joined:
    Dec 12, 2020
    Posts:
    22
    Thank you guys for the help!
     
    Deleted User likes this.