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

Problem with loading screen

Discussion in 'Scripting' started by tepozki, Apr 2, 2020.

  1. tepozki

    tepozki

    Joined:
    Mar 22, 2020
    Posts:
    8
    Hey.

    I have a problem with loading screen. My loading bar wont move at all when loading screen pops up. here is my code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class SceneLoader : MonoBehaviour
    7. {
    8.     public GameObject loadingScreen;
    9.     public Slider slider;
    10.  
    11.  
    12.     public  void LoadNextScene(int sceneIndex)
    13.     {
    14.  
    15.         StartCoroutine(LoadAsyncOperation(sceneIndex));
    16.     }
    17.  
    18.     IEnumerator  LoadAsyncOperation(int sceneIndex )
    19.     {
    20.      
    21.    
    22.  
    23.         loadingScreen.SetActive(true);
    24.  
    25.         AsyncOperation gamelevel = SceneManager.LoadSceneAsync(4);
    26.         gamelevel.allowSceneActivation = false;
    27.         while (gamelevel.isDone == false)
    28.         {
    29.             float progress = Mathf.Clamp01(gamelevel.progress / .9f);
    30.             //Time.timeScale = 0.2f;
    31.             slider.value = gamelevel.progress;
    32.             if (gamelevel.progress == 1f)
    33.             {
    34.                 slider.value = 1f;
    35.                 gamelevel.allowSceneActivation = true;
    36.                 loadingScreen.SetActive(false);
    37.             }
    38.          
    39.             yield return null;
    40.         }
    41.      
    42.     }
    43.  
    44. }
    also i have scene handler script as well
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine.UI;
    5.  
    6. public class SceneHandler : MonoBehaviour
    7. {
    8.  
    9.  
    10.     void Awake()
    11.     {
    12.         DontDestroyOnLoad(gameObject);
    13.     }
    14.  
    15.     public void SaveScene()
    16.     {
    17.         int activeScene = SceneManager.GetActiveScene().buildIndex;
    18.  
    19.         PlayerPrefs.SetInt("ActiveScene", activeScene);
    20.     }
    21.  
    22.     public void LoadScene()
    23.     {
    24.         int activeScene = PlayerPrefs.GetInt("ActiveScene");
    25.  
    26.         //SceneManager.LoadScene(activeScene);
    27.  
    28.         //Note: In most cases, to avoid pauses or performance hiccups while loading,
    29.         //you should use the asynchronous version of the LoadScene() command which is: LoadSceneAsync()
    30.  
    31.         //Loads the Scene asynchronously in the background
    32.         StartCoroutine(LoadNewScene(activeScene));
    33.     }
    34.  
    35.     IEnumerator LoadNewScene(int sceneBuildIndex)
    36.     {
    37.         AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneBuildIndex);
    38.      
    39.         asyncOperation.allowSceneActivation = false;
    40.  
    41.         while (asyncOperation.progress < 0.9f)
    42.         {
    43.          
    44.             yield return null;
    45.         }
    46.  
    47.         asyncOperation.allowSceneActivation = true;
    48.  
    49.     }
    50. }
    this handles autosave. could it have something to do with my scene loader not working??
     
    Last edited: Apr 2, 2020
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    What I would suggest you do is add a debug right above the if statement that prints out the value of gamelevel.progress
    This would be your first step in debugging your own code.

    As a general rule of thumb, comparing floats for equality is the wrong way to go about it. This deals with the floating point error which gets mentioned on the forums a lot. There is a command that checks if the two floats are approximately equal, but you could also just do something like your while statement and check if your progress is < an amount or once it's > an amount and that would probably be enough.

    But, to start, add that debug and see what values you are getting.
     
  3. tepozki

    tepozki

    Joined:
    Mar 22, 2020
    Posts:
    8
    Okay i got the loading bar to work but its still not perfect. Now i'm trying to load a bigger scene and the bar's (slider's) value goes up to 0.9 and it shows that the scene is loading in the hierarchy, but it gets stuck there. so it wont finish the loading.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    Yeah, the way Unity does the .progress value of AsyncOperation is insane bullshit. If you set allowSceneActivation to false, it won't progress beyond .9f. .9 is a magic value that means "I've done everything I can do without you setting allowSceneActivation to true".