Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

While in Coroutine Unity Crash

Discussion in 'Scripting' started by wechat_os_Qy06hIF5DiUpguLJT5P_Qi-Wk, Feb 11, 2021.

  1. wechat_os_Qy06hIF5DiUpguLJT5P_Qi-Wk

    wechat_os_Qy06hIF5DiUpguLJT5P_Qi-Wk

    Joined:
    Jan 24, 2020
    Posts:
    9
    I'm trying to loop through a bunch of files inside an URL folder, when i use a "for" loop, it works perfectly fine, but when i use a "do while" or "while" Unity crashes, i tested the CheckURL method and it works as expected.

    Code (CSharp):
    1.  public static IEnumerator DownloadIcons()
    2.     {
    3.  
    4.         int i = 0;
    5.  
    6.         string url = Preloader.base_url + "/src/icons/" + i + ".png";
    7.  
    8.         while (CheckUrl(url))
    9.         {
    10.             UnityWebRequest request = UnityWebRequestTexture.GetTexture(url);
    11.             yield return request.SendWebRequest();
    12.  
    13.             while (!request.downloadHandler.isDone)
    14.                 yield return null;
    15.  
    16.             if (request.downloadHandler.isDone)
    17.             {
    18.  
    19.                 Texture2D tex = ((DownloadHandlerTexture)request.downloadHandler).texture;
    20.                 Preloader.list_icons.Add(SrcManager.WriteTexture(tex));
    21.                 i++;
    22.                 url = Preloader.base_url + "/src/icons/" + i + ".png";
    23.                 yield return null;
    24.             }
    25.  
    26.         }
    27.  
    28.  
    29.     }
    30.  
    31.  
    32.     public static bool CheckUrl(string url)
    33.     {
    34.  
    35.         WebRequest webRequest = WebRequest.Create(url);
    36.         WebResponse webResponse;
    37.         try
    38.         {
    39.             webResponse = webRequest.GetResponse();
    40.         }
    41.         catch //If exception thrown then couldn't get response from address
    42.         {
    43.             return false;
    44.         }
    45.         return true;
    46.  
    47.     }
    any ideas ?
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,640
    Are you sure your checks are correct? I think you just end up with a never ending loop and eventually get out of memory.
    Also, it is very inefficient to do a web request to check the url work and then do another request to download. You could do it all using UnityWebRequest and then checking if it didn't end in error.
     
    GroZZleR likes this.
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You never stop the loop. You're effectively saying while(true) as long as there are no errors. You need to break out of the while loop later on, or return from the function entirely when the icon is downloaded.
     
  4. wechat_os_Qy06hIF5DiUpguLJT5P_Qi-Wk

    wechat_os_Qy06hIF5DiUpguLJT5P_Qi-Wk

    Joined:
    Jan 24, 2020
    Posts:
    9
    Even though i'm pretty sure my checks are fine, you got a good point, i'd rather wait for an error from UnityWebRequest than doing this nonsense, i think i was a bit frustrated i wasn't thinking clearly.
    Thanks a lot, i'll double check!
     
  5. wechat_os_Qy06hIF5DiUpguLJT5P_Qi-Wk

    wechat_os_Qy06hIF5DiUpguLJT5P_Qi-Wk

    Joined:
    Jan 24, 2020
    Posts:
    9
    When (CheckURL) returns false, the loop should be stopped no ? am i getting it wrong ?
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,640
    Yes, that should exist the loop and the coroutine too, since there's nothing after the loop.