Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by julianilevy · Aug 11, 2017 at 07:49 PM · scenestartasyncawakeadditive

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges