Page 1 of 1

problem with loading bar and SaveSystem.currentAsyncOperation.progress

Posted: Tue Feb 22, 2022 9:10 am
by OddMan2
Hello again,
i'm trying to get two MMTools componentsto show the SaveSystem loading progress value,

The components have a SetProgress(float) method by wich you can update the progress bar and percentage.
both ui elements and the scripts are on the scene fader canvas.

my problem is that i am not using a loading scene, and if i call SaveSystem.currentAsyncOperation.progress outside a loading scene, i get this error:

Code: Select all

NullReferenceException: Object reference not set to an instance of an object
LoadingScreenProgressBar+<UpdateProgressBar>d__5.MoveNext ()
to add more context: im using the OnTransitionstartEvent on the transition manager to start my updateBar() coroutine.

this is the code for my loading bar

Code: Select all

using System.Collections;
using UnityEngine;
using PixelCrushers;
using MoreMountains.Tools;

public class LoadingScreenProgressBar : MonoBehaviour
{
   //MM tools references
    public MMSceneLoadingImageProgress loadingImage;
    public MMSceneLoadingTextProgress loadingText;

    // params
    private float progressValue;

    public void UpdateBar()  {StartCoroutine(UpdateProgressBar()); }
     public IEnumerator UpdateProgressBar()
        {
        
            while (!SaveSystem.currentAsyncOperation.isDone)
            {

            progressValue = SaveSystem.currentAsyncOperation.progress; 
        
             
            Debug.LogWarning("progress: " + SaveSystem.currentAsyncOperation.progress);
               
                
                    if (loadingImage)
                    {
                        loadingImage.SetProgress(progressValue);
                    }
                    if (loadingText)
                    {
                        loadingText.SetProgress(progressValue);
                    }
             

                yield return null;
            }
        }

}

Re: problem with loading bar and SaveSystem.currentAsyncOperation.progress

Posted: Tue Feb 22, 2022 9:22 am
by Tony Li
Hi,

Are you loading scenes using More Mountains' MMLoadingSceneManager or the Pixel Crushers Save System?

SaveSystem.currentAsyncOperation is only valid if you're loading scenes using the Pixel Crushers Save System.

Re: problem with loading bar and SaveSystem.currentAsyncOperation.progress

Posted: Tue Feb 22, 2022 9:28 am
by OddMan2
i am using the Pixel Crushers Save System, sorry about the confusion.

Re: problem with loading bar and SaveSystem.currentAsyncOperation.progress

Posted: Tue Feb 22, 2022 11:12 am
by Tony Li
SaveSystem.currentAsyncOperation will be null until it starts loading the destination scene. This means that while it's loading the loading scene it will still be null. Once the loading scene is loaded, it will be valid while it loads the destination scene. These code changes should work:

Code: Select all

using System.Collections;
using UnityEngine;
using PixelCrushers;
using MoreMountains.Tools;

public class LoadingScreenProgressBar : MonoBehaviour
{
   //MM tools references
    public MMSceneLoadingImageProgress loadingImage;
    public MMSceneLoadingTextProgress loadingText;

    // params
    private float progressValue;

    public void UpdateBar()  {StartCoroutine(UpdateProgressBar()); }
     public IEnumerator UpdateProgressBar()
        {
        
            while (SaveSystem.currentAsyncOperation == null || !SaveSystem.currentAsyncOperation.isDone)
            {

            progressValue = (SaveSystem.currentAsyncOperation != null) ? SaveSystem.currentAsyncOperation.progress : 0; 
        
             
            // Debug.LogWarning("progress: " + SaveSystem.currentAsyncOperation.progress);
               
                
                    if (loadingImage)
                    {
                        loadingImage.SetProgress(progressValue);
                    }
                    if (loadingText)
                    {
                        loadingText.SetProgress(progressValue);
                    }
             

                yield return null;
            }
        }

}

Re: problem with loading bar and SaveSystem.currentAsyncOperation.progress

Posted: Tue Feb 22, 2022 12:12 pm
by OddMan2
Right, that fixed it. thank you!

Re: problem with loading bar and SaveSystem.currentAsyncOperation.progress

Posted: Tue Feb 22, 2022 1:20 pm
by Tony Li
Glad to help!