- Home /
 
How to have the scene load after a few seconds
Hello all,
I have a loading screen in my project and I want it to appear for a few seconds while unity load the next scene asynchronously. this is the original method I had:
     IEnumerator LoadAsynchronously(int currentSceneNumber)
     {
         AsyncOperation operation = SceneManager.LoadSceneAsync(currentSceneNumber);
         loadingScreen.SetActive(true);
         while (!operation.isDone)
         {
             float progress = Mathf.Clamp01(operation.progress / .9f);
             progressText.text = progress * 100f + "%";
             yield return null;
         }
         loadingScreen.SetActive(false);
         canvas.worldCamera = Camera.main;
     }
 
               I need the script to do the following:
on button press: enable loading screen and start loading the next scene but not yet activate it
start the 2 seconds timer
disable the loading screen and activate the next scene after 2 seconds
Answer by Hellium · May 26, 2019 at 10:03 AM
  IEnumerator LoadAsynchronously(int currentSceneNumber)
  {
      float loadingStartTime = Time.time;
      AsyncOperation operation = SceneManager.LoadSceneAsync(currentSceneNumber);
      loadingScreen.SetActive(true);
      while (!operation.isDone || (Time.time - loadingStartTime) < 2f)
      {
          float progress = Mathf.Clamp01(operation.progress / .9f);
          progressText.text = progress * 100f + "%";
          yield return null;
      }
      loadingScreen.SetActive(false);
      canvas.worldCamera = Camera.main;
  }
 
              Thanks @Hellium for the quick reply. I don't see bigger difference when using this method. I guess what really counts here is the $$anonymous$$athf.Clamp01 and the fact that even when asyncloading the loading it is making app "lag"
$$anonymous$$y goal is to achieve async loading while the player doesn't "feel" the loading. Assume that the loading will take up to two seconds. On button press - immediately set active the "loading screen" and let it load for two seconds - see the progress text output go from 0%- 100% during this time. $$anonymous$$eanwhile load the scene async but do this while the % of loading shown.
$$anonymous$$y best solution would be that there two variables: float = 2f or actual operation.progress duration.
Check which is longer and choose this one.
this way I ensure that the loading screen appears for at least two seconds and on the other hand if the loading of the scene takes longer than I still have time with the loading screen.
It seems I've made a mistake in the condition of the while. Try to replace && by ||. 
Hi @Hellium replacing && with || can actually be tricky because or condition means that if one of the conditionals is correct then while loop will play... right? Trying that.
I think it's best to find a way to condition the while loop with fixed time also I wonder whether I can know the operation.progress ahead
and also use the  operation.allowSceneActivation = false;  and  operation.allowSceneActivation = true; 
Your answer
 
             Follow this Question
Related Questions
How to select scene based on device screen size? 1 Answer
LoadSceneMode.Single seems to be not working. 0 Answers
Loading Screen? 6 Answers
Load all scenes on begning.... 1 Answer
Why does loading the scene not work? 2 Answers