problem with loading bar and SaveSystem.currentAsyncOperation.progress

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
OddMan2
Posts: 29
Joined: Fri Feb 11, 2022 11:14 am

problem with loading bar and SaveSystem.currentAsyncOperation.progress

Post 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;
            }
        }

}
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

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

Post 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.
OddMan2
Posts: 29
Joined: Fri Feb 11, 2022 11:14 am

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

Post by OddMan2 »

i am using the Pixel Crushers Save System, sorry about the confusion.
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

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

Post 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;
            }
        }

}
OddMan2
Posts: 29
Joined: Fri Feb 11, 2022 11:14 am

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

Post by OddMan2 »

Right, that fixed it. thank you!
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

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

Post by Tony Li »

Glad to help!
Post Reply