- Home /
How to make Awake and Start get called in order with additive scenes?
Hello!
I'm making a Scene Loader to set up my game from multiple additive scenes, like little components added to the main scene (Lighting Scene, Scenary Scene, Gameplay Scene, etc.). I managed to activate all of them simultaneously, but when they do the Awake and Start methods do not trigger in the correct order. i.e:
Awake and Start from scene 1 are called at frame 4.
Awake and Start from scene 2 are called at frame 5.
And so on...
void Start()
{
LoadScenes(scenesPaths, LoadedAtAwake);
}
void LoadScenes(List<string> paths, LoadSceneCallback callback)
{
_scenesCount = paths.Count;
_loadingScenes = new List<AsyncOperation>();
var activeScenes = GetActiveScenes().Select(scene => scene.path);
// Load all scenes not already active
foreach (var path in paths)
{
if (!activeScenes.Contains(path))
{
AsyncOperation scene = SceneManager.LoadSceneAsync(path, LoadSceneMode.Additive);
scene.allowSceneActivation = false;
_loadingScenes.Add(scene);
StartCoroutine(LoadProgress(scene, callback));
}
}
}
IEnumerator LoadProgress(AsyncOperation scene, LoadSceneCallback callback)
{
// Load the selected scene in additive mode and wait for all scenes to
// be loaded before activating them
// Wait till the magic .9 number when the scene is ready to be loaded
while (scene.progress < 0.9f)
yield return null;
_scenesLoadedAmount++;
if (_scenesLoadedAmount == _scenesCount)
{
// If all scenes are ready to be loaded allow all of them to be loaded.
foreach (var async in _loadingScenes)
async.allowSceneActivation = true;
callback();
}
}
Any ideas if it's possible to achieve that every Awake get called first and then every Start, once all additive scenes were enabled?
Answer by brinca · Aug 19, 2018 at 12:05 PM
Well, Awake and Start are called as the corresponding scripts are instantiated, which happens only after each scene is fully loaded, which is dependent on many factors (size of the data being loaded, number of objects/scripts, number of concurrent read operations, device type, etc), so you can't reliably expect those callbacks to be executed at a specific time.
However, what you can do is have each instance add itself to a shared list (e.g. on your loader script), and then iterate that list to call your own init method, after all scenes have finished loading.
Supposing your loader class is called Loader
, it could have something like:
public static List<MonoBehaviour> Instances = new List<MonoBehaviour>();
// This method should be called when all scenes have finished loading
private void OnAllScenesLoaded(){
foreach (var instance in Instances)
instance.SendMessage("Init");
Instances.Clear();
}
And then on each of your scripts you would call:
void Awake(){
Loader.Instances.Add(this);
}
void Init(){
// Do your initialization here
}
If you don't like SendMessage (I don't), you can use an interface based approach, but the core idea remains the same.
Your answer
Follow this Question
Related Questions
Scene starts before scene loading is finished 0 Answers
Something like Awake that runs even if disabled? 11 Answers
Display loading/progress indicator from Start()? 0 Answers
DontDestroyOnLoad() not calling awake/start again 2 Answers
Object reference becomes null between Awake and Start after scene load 2 Answers