Search Unity

Do while doesnt work with async.progress

Discussion in 'Scripting' started by justadeveloper, Nov 26, 2015.

  1. justadeveloper

    justadeveloper

    Joined:
    Jan 12, 2014
    Posts:
    33
    I want to load my scene when async.progress is 0.9,but first of all,i want to run my loading screen text at least once,but i dont know why it doesnt work

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class LoadScreenScript : MonoBehaviour
    6. {
    7.     public Text DoYouKnow;
    8.     public Text loadingText;
    9.  
    10.  
    11.     public int imageNumber;
    12.     public float letterPause;
    13.  
    14.     public string sceneToLoad;
    15.     string messageToAnimate;
    16.     // Use this for initialization
    17.  
    18.     void Start ()
    19.     {
    20.         messageToAnimate = ".......";
    21.         loadingText.text = "Loading";
    22.  
    23.         changeDoYouKnowText ();
    24.         StartCoroutine (LoadingScreen ());
    25.     }
    26.    
    27.     // Update is called once per frame
    28.     void Update ()
    29.     {
    30.        
    31.     }
    32.  
    33.     void changeDoYouKnowText()
    34.     {
    35.         imageNumber = Random.Range (1, 5);
    36.         Debug.Log (imageNumber);
    37.         if (imageNumber == 1)
    38.             DoYouKnow.text = "hello mikel";
    39.         else if (imageNumber == 2)
    40.             DoYouKnow.text = "hello erick";
    41.         else if (imageNumber == 3)
    42.             DoYouKnow.text = "hello kevin";
    43.         else if (imageNumber == 4)
    44.             DoYouKnow.text = "hello world";
    45.     }
    46.  
    47.     IEnumerator LoadingScreen ()
    48.     {
    49.         AsyncOperation async = Application.LoadLevelAsync (sceneToLoad);
    50.         Debug.Log(async.progress);
    51.         async.allowSceneActivation = false;
    52.         do{
    53.             foreach (char letter in messageToAnimate.ToCharArray())
    54.             {
    55.                 loadingText.text += letter;
    56.                 yield return 0;
    57.                 yield return new WaitForSeconds (letterPause);
    58.             }
    59.             loadingText.text = "Loading";
    60.             Debug.Log(async.progress);
    61.         }while(async.progress<0.9);
    62.         async.allowSceneActivation = true;
    63.     }
    64.  
    65.  
    66. }
    67.  
     
  2. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    It is quite possible that your scene loads instantly, so it never manages to update your text. You can try to fake loading though, like yield 1 second to animate text, then try to LoadLevelAsync() and do the loop. That way, if your scene loads instantly you will still display animated text once, as well as cover huge scenes that take some time to load.

    Hope it helps!