Search Unity

yield return does not wait until the a WWW text link is being readed

Discussion in 'Scripting' started by kordou, Mar 29, 2018.

  1. kordou

    kordou

    Joined:
    May 16, 2017
    Posts:
    13
    HI, i need some help please here, I have a simple code like that

    Code (CSharp):
    1. String mywebtext;
    2. string mylink = "my link of the file goes here" ;
    3. public void readdatafromweb()
    4. {
    5.      StartCoroutine (readthefile (mylink));
    6.       // and then call a function that manipulates mywebtext to split it with some chars,
    7. }
    8. IEnumerator readthefile (string url)
    9.      {
    10.          WWW MyTXT= new WWW (url);
    11.          yield return MyTXT;
    12.                  mywebtext = MyTXT.text;
    13.      }
    my problem is that even i have a yield return the code proceed to the next funtion before the download is finished and the 'mywebtext' gets a value so i get an error or www not finished

    i also tried
    Code (CSharp):
    1.  yield return new waitUntil(()=> MyTXT.isdone == true);
    but also the same issue
    can someone tell how can i proceed when and only the mywebtext gets the text of the link;

    thanks in advanced
     
  2. JoMaHo

    JoMaHo

    Joined:
    Apr 2, 2017
    Posts:
    94
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    From the comment in your code, are you trying to access the string (and/or call a method) before the coroutine is finished?
    If that is the case -- a common mistake with coroutines for new comers -- add the method at the end of the coroutine. :)

    Code that comes after 'StartCoroutine' does not wait for the coroutine to finish. The coroutine will run up until its first yield, and then any code that followed the 'StartCoroutine' will be executed...
     
  4. kordou

    kordou

    Joined:
    May 16, 2017
    Posts:
    13
    Hi, I want to run the next routine after the www download is finished and only then but without blocking the rest of the game if possible. the upper code starts the WWW but does not wait to finish the download in order to initiate the next routine and for that reason i have an error because i want to read a variable (WWW.Text) that it is not finished from the WWW.
     
  5. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    The code after "yield return" will execute after download is finished, so anything you want to happen after download ends should be put there.
     
  6. kordou

    kordou

    Joined:
    May 16, 2017
    Posts:
    13
    found the solution so writing to anyone that needs it in the future

    first create a Cooroutine calling the WWW
    Inside the IEnumerator after calling the WWW put a Yield return and the WWW variable

    then run a (while WWW. progress <1){
    debug.log("downloading);
    }

    and then the next part of code.


    the While loop waits until the file is being downloaded 100%. So it stops all the code.

    And the yield will run anyother code if needed after the downloaed is finished (but without stoping the code. As parraler);



    thanks a lot guys :)