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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Scene takes long time to load, even when Coroutine is applied(loading screen too)

Discussion in 'Scripting' started by phantom_x404, May 1, 2020.

  1. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    I am making an infinite runner game, it has three levels, easy, medium, hard, and a main menu where we can choose the difficulty.
    But, the level hard takes a really long time to load up(around 3 seconds), while the others load instantly, so I set up a loading screen which is displayed synchronously while the game scene is loading up. In the loading screen there is a loading bar and that is pretty much it, the loading bar fills up as the screen loads.
    The loading also works perfectly for Easy, and Medium, but for some reason, whenever I click on the HARD LEVEL, it lags for around a second(stays in main menu), then goes into loading screen with the loading bar directly at 100%, and then it again lags for around a second before starting the game. I have tried to implement this Loading screen on the RESTART button for the HARD LEVEL also, and for the restart button, it does not even display it, it just lags for a second and directly takes me to the level. Again, this only happens for the HARD LEVEL.
    The code for the loading screen is below:
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine.UI;
    5.  
    6. public class LevelLoader : MonoBehaviour
    7. {
    8.  
    9.     public GameObject loadingScreen;
    10.     public Slider slider;
    11.     public Text progressText;
    12.  
    13.     public void LoadLevel(int sceneIndex)
    14.     {
    15.         StartCoroutine(LoadAsynchronously(sceneIndex));
    16.        
    17.     }
    18.  
    19.     IEnumerator LoadAsynchronously(int sceneIndex)
    20.     {
    21.         AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);
    22.  
    23.         loadingScreen.SetActive(true);
    24.  
    25.         while (!operation.isDone)
    26.         {
    27.             float progress = Mathf.Clamp01(operation.progress / 0.9f);
    28.            
    29.             slider.value = progress;
    30.             progressText.text = progress * 100f + "%";
    31.  
    32.             yield return null;
    33.         }
    34.     }
    35.    
    36. }
    37.  
    Any help with this issue would be very appreciated!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    You already have a thread posted for this topic. Don't spam a new one.
     
  3. phantom_x404

    phantom_x404

    Joined:
    Apr 26, 2020
    Posts:
    26
    This is a bit different from that, that one is for animation, this is for load screen.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    The answer is the same though: attach the profiler and find what is taking all the time.
     
    matkoniecz likes this.