LoadSceneAsync not triggering Awake functions

Hey everyone,

I have a loading bar script that uses LoadSceneAsync and allowSceneActivation = false to load the next scene while in a loading screen. Here’s the code in question:

public class LoadingManager : MonoBehaviour
{
    [SerializeField] private ProgressSettings _progressSettings;

    private bool isDataLoaded;
    
    void Start()
    {
        StartCoroutine(LoadScene());
        LoadData();
    }

    private void LoadData()
    {
        if (_progressSettings.CurrentWorld.IsFighting)
        {
            isDataLoaded = true;
            return;
        }
        
        // TODO: Load data for each game
        // switch (_progressSettings.CurrentWorld.WorldType)
        // {
        //     case WorldType.Crafting:
        //         _gameDataLoader.Load();
        //         isDataLoaded = true;
        //         break;
        // }
    }

    private IEnumerator LoadScene()
    {
        // make sure to wait at least 1 second so the loading screen doesn't just flicker
        while (!isDataLoaded)
        {
            yield return new WaitForSeconds(2);
        }

        if (_progressSettings.CurrentWorld.IsFighting)
        {
            AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("Fight");
            asyncOperation.allowSceneActivation = false;
            while (asyncOperation.progress < 0.9f || !_progressSettings.IsDoneScanning)
            {
                yield return null;
            }
            asyncOperation.allowSceneActivation = true;
        }
        else
        {
            SceneManager.LoadScene(_progressSettings.CurrentWorld.SkillingSceneIndex);
        }
    }
    
    
}

In this case _progressSettings.IsDoneScanning is set to true in the Awake method of a script in the scene that’s being loaded. The problem I’m running into is that the variable is never being set, because the awake method is never being called. I was under the impression that awake was called as part of the async operation even while allowSceneActivation is false, and I’ve seen several code examples that confirm this. Is there a reason Awake isn’t being called during the loading time in this case? I can’t use additive loading because it causes problems with events in the next scene.

3 Answers

3

I would be extremely surprised to hear that awake gets called before allowsceneactivation is set to true. Can you share links to the documentation that you mentioned?

It's hard to find concrete documentation on it, but I've seen a few forum posts such as: https://discussions.unity.com/t/loading-bar-for-awake-function/223554 which talk about awake functions factoring into their loading time. I also had a previous project where I ran into an issue using this exact same loading script where I had to move some code into start because awake was being called from the async load "Awake is called when the script instance is being loaded." so it makes sense that awake would be called during Async loading and Start wouldn't be called until the scene activates.

–

You haven‘t posted the entire script. Thus I‘m not sure if you followed the requirement of setting allowSceneActivation in a coroutine as the manual states.

I added the full script for context. I read the manual, and I BELIEVE I followed it properly but I may have misunderstood or overlooked something

–

It seems, you are getting messed up. You are starting a co-routine in start method and immediately calling function “LoadData”. There are 2 possibilities I am thinking would be there.

  1. You are checking condition “_progressSettings.CurrentWorld.IsFighting” in “LoadData” method. Here if this condition is not true, then your “isDataLoaded” boolean will never get true as “LoadData” method is only being called once.
  2. if “_progressSettings.CurrentWorld.IsFighting” is getting set to true in your next scene then, you need to call “LoadData” again in “While(!isDataLoaded)” loop. That way, it is possible to get this condition false and it will move next.

_progressSettings.CurrentWorld.IsFighting is just framework for having multiple minigames that can be loaded in the future. Currently it's always set to true. The while loop in the coroutine you referenced currently only exists to force the loading screen to play for 2 seconds so that it doesn't flicker with fast loading times. I used breakpoints to verify that the entire coroutine is being run, but IsDoneScanning is never set to true because the awake function it's set in never gets called, even though the next scene is successfully loading to Unity's arbitrary 90%.

–

It seems, I am not sure from which script awake function should be called. so can't help you on this.

–