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.
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.
– audiodreadofficial