Slider not moving in Loading Screen

I have setup code for a loading screen, which works and loads the next scene when I press any key. However the slider does not move. I am uncertain if I did something wrong, I followed a tutorial on how to make a loading screen but removed the button that triggers it.

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class NewBehaviourScript : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        StartCoroutine(AsynchronousLoad(1));
    }

    // Update is called once per frame
    void Update()
    {



    }

    bool canPlayerFire = true;
    IEnumerator coRoutineTest()
    {
        Debug.Log("Fire");

        canPlayerFire = false;

        yield return new WaitForSeconds(3);

        canPlayerFire = true;

    }
    IEnumerator AsynchronousLoad(int lvl)
    {
        yield return null;

        AsyncOperation ao = SceneManager.LoadSceneAsync(lvl);
        ao.allowSceneActivation = false;

        while (!ao.isDone)
        {
            // [0, 0.9] > [0, 1]
            float progress = Mathf.Clamp01(ao.progress / 0.9f);


            // Loading completed
            if (ao.progress == 0.9f)
            {
                Debug.Log("Press a key to start");
                if (Input.anyKey)
                    ao.allowSceneActivation = true;
            }

            yield return null;
        }
    }
}

Like I said the slider doesn’t move at all, any help to fix this would be appreciated.

I think I’m blind…what slider? Am I missing where you are getting or setting a slider and then applying a value to it?

Aw, I had exactly the same thought… Sorry, OP, you have not added a slider to the script/code! :slight_smile:

Maybe you want to add the Slider and set its value to the ‘progress’ inside the coroutine? :slight_smile:

Oh My Gosh, I feel silly. Your right I forgot this Snippet of Code. Thank you Guys for pointing that out.

public GameObject GameObject;
public Slider slider;
AsyncOperation async;

:slight_smile: Glad ya got it fixed up.

Well I thought I had gotten it fixed up, I re wrote the code per the tutorial exactly as I saw on youtube. However when I run it with the loading screen object and slider set in the unity inspector as shown
3279890--253708--loading screen.PNG
and when I click play, my slider still does not move, This is my rewritten code. I do not know if I am missing something or not but upon investigation it looks like I did everything right.

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class NewBehaviourScript : MonoBehaviour
{
   public GameObject LoadingScreenObj;
   public Slider slider;

    AsyncOperation async;
   void start()
    {

    }
    public void LoadingScreenexample()
    {
        StartCoroutine(LoadingScreen(0));
    }
 
    IEnumerator LoadingScreen(int lvl)
    {
    
        LoadingScreenObj.SetActive(true);
        async = SceneManager.LoadSceneAsync(lvl);
        async.allowSceneActivation = false;

        while (async.isDone == false)
        {
            slider.value = async.progress;
            if (async.progress == 0.9f)
            {
                slider.value = 1f;
                async.allowSceneActivation = true;
            }
            yield return null;
        }


    }

I could be wrong though and missed something :eyes:

On your slider, is whole numbers unchecked?

Yes, Use Whole numbers is unchecked.

Not sure, looks better this time. If you debug log the async progress do you get more than 1 value (ie: is it going by so fast you can’t see it? The second code you posted doesn’t include the Input.anyKey from your previous post - unless you just left that out on the forum…

How is your loading starting since you removed the coroutine call from Start?

when I debug the async I get no output from it at all…=\

I Just moved it to start with no change in results.

When I saw the method calling the coroutine, I imagined he was calling it from elsewhere. :slight_smile:
So, you’re saying you don’t see any debug output in the console at all?

Did you fix the typo for Start (shouldn’t be ‘start’) in your personal copy of the code? :slight_smile:

Yeah, I thought he might have also. But figured I’d ask. @MrHammy36 But yep, Start vs start are different.

there we go, It’s fixed my issue was the start and not calling it from it.
Thanks Guys your help is appreciated.

No, I hear ya… doesn’t hurt to ask/check :slight_smile:

No problem. And of course you don’t have to call it from Start… ( I mean, maybe in your code it makes the most sense). What you do have to do, though, is call it somehow :slight_smile: :slight_smile:

Glad you got it working.