LoadSceneAsync with a ui button

Hey, I am wanting to load the next scene as soon as my game starts. I dont want the scene to change automatically once it is loaded though. I have a UI play button that I want the player to need to click before the game progresses to the next scene. How do i do this with a UI button. I currently have it working automatically when the scene is loaded, and have found code to do it with a keypress but nothing involving a UI button, is this possible?

This is the code that is working automatically which is called with a coroutine

   IEnumerator LoadNextScene()
    {
        asyncLoad = SceneManager.LoadSceneAsync("imagePicker");
        asyncLoad.allowSceneActivation = false;
        while (!asyncLoad.isDone)
        {
            yield return null;
        }

    }

You’d work with the UI buttons just like any other UI button. It depends on which UI system you’re using, but we’ll assume the default one.

Coincidentally, I just saw this video, looks like it covers pretty much all kinds of things related to loading screens for you to check out.

An important thing to think about here is that these are two completely separate problems:

  • Waiting to change scenes until something happens
  • Detecting a UI button press (or a keypress, or the user yelling at their computer, or…)

So, all you really need to do is figure out how to run a function in response to a button being pushed – there’s nothing specific to the first problem that makes the second problem any harder!

I know how to detect a button press etc, that is all working fine. But I want the loasSceneAsync to start as soon as the first scene starts, but not load until the button is pressed, but I cant find how to stop the next scene starting until a button is pressed, ive tried setting a bool until the button is pressed but that does work either. This is what I am working with

private void Start()
    {
     
        StartCoroutine(LoadNextScene());
    }

    public void switchScenes()
    {
        isLoaded = true;


    }

IEnumerator LoadNextScene()
    {
        asyncLoad.allowSceneActivation = false;
        if (isLoaded)
        {
            Debug.Log("isloaded");
            asyncLoad.allowSceneActivation = true;
        }
        asyncLoad = SceneManager.LoadSceneAsync("imagePicker");
        while (!asyncLoad.isDone)
        {
            yield return null;
        }

    }

The switchScenes function, is what is called with the button press

I have the vaguest recollection that this (allowSceneActivation) doesn’t work in editor but works fine in build. I don’t see a note like that in the docs though… Did you try it in build?

Don’t you just need to move the if check in your coroutine so that it is inside the while loop?
At the moment it will start the coroutine, set the allowSceneActivation to false, and isLoaded will be false so the if statement will not be used, and then the coroutine will end up in the while loop where all it will ever do is yield return null each tick.
Even if you click the button which sets the isLoaded variable to true, the coroutine has already gone past that point where that would then be used to set the allowSceneActivation to true.
I’m thinking a change like this:

IEnumerator LoadNextScene()
    {
        asyncLoad = SceneManager.LoadSceneAsync("imagePicker");
        asyncLoad.allowSceneActivation = false;
        while (!asyncLoad.isDone)
        {
           if (isLoaded && !asyncLoad.AllowSceneActivation)
           {
               Debug.Log("isloaded");
               asyncLoad.allowSceneActivation = true;
           }
            yield return null;
        }
}

Of course, this is untested and I haven’t worked much with async loading in the past - but it just seemed to me that there was nothing actually setting the allowSceneActivation other than right at the start of the coroutine, but it was needed later on.

Have a look over here. It’s non-obvious…you wait for the AsyncOperation to be 90% complete. It won’t actually hit 100% until you allow the scene to activate.

Thanks everyone for all the help and links, managed to get it to work using BABIA_GameStudio solution.