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 IngeJones · Nov 23, 2017 at 04:13 PM · scripting probleminstantiateevent

Gamecontroller "Awake" instantiates other objects - Event order question

I load a scene where the only object is my game controller. During its Awake() it loads some saved data and instantiates a lot of other objects in their saved locations.

These instantiated objects have to do some self-setting up in their Awake() and after that the Gamecontroller must do something for each of them based on the values the objects have set up for themselves, which I was hoping to put in the controller's Start()

Given that the saved objects are instantiated during the controller's awake(), and the self-setting up is done during the saved objects' awake(), can I be reassured that all this will be completed before the controller gets to doing its own Start()?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by pako · Nov 23, 2017 at 07:54 PM

@Ingejones you say that:

During its Awake() it loads some saved data and instantiates a lot of other objects

I haven't seen your code, but it seems that before the game controller's Awake() completes, the saved objects will be instantiated. When they get intantiated (apparently in an "Active" state), their Awake() will run to completion, before game controller's Awake() completes. This also means that the saved objects' OnEnable() will also run immediately after each object's Awake(), and after all these Awake()/OnEnable() sequences, the game controller's Awake() will complete, followed by its OnEnable() and then, before the first Update() all Start() methods will run. Of course, if you don't have any code in any OnEnable() there's nothing to run there.

To put it simply, the game controller's Awake() cannot complete before all instantiations made inside this Awake() complete, and theses instantiations will only complete, after all relative Awake()s complete. So, yes, I assure you that all of the saved objects Awake() methods will be completed, before the controller's Start(). It's very easy to test this yourself. Just put in each script's Awake() and Start() Debug.Log statements e.g.:

 void Awake(){
 
 //At the start of the method
 Debug.Log(name + " - Awake() Started.");
 
 //code...
 
 //At the end of the method
 Debug.Log(name + " - Awake() Completed.");
 
 }
 
 
 void Start(){
 
 //At the start of the method
 Debug.Log(name + " - Start() Started.");
 
 //code...
 
 //At the end of the method
 Debug.Log(name + " - Start() Completed.");
 
 }









Comment
Add comment · Show 1 · 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
avatar image IngeJones · Nov 23, 2017 at 09:10 PM 0
Share

Thanks @pako. I did in fact test with debug logging and got the results as you say, but I just wanted reassurance that it was a predictable result and not just some lucky coincidence related to this particular context, that everything was showing up in the desired order. I had read so many references to "indeter$$anonymous$$ate order" etc that I didn't want to base my entire plans on something that might suddenly up-end itself!

avatar image
0

Answer by jeffreyrampineda · Nov 23, 2017 at 04:24 PM

It says on the reference that "Awake is always called before any Start functions" https://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html

Comment
Add comment · Show 1 · 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
avatar image IngeJones · Nov 23, 2017 at 04:57 PM 0
Share

Yes for objects that are already on the scene in editor. However in my case all but one of these are instantiated at runtime, during the original object's Awake(). The manual, which I did read before posting there, says "Note that for objects added to the scene, the Awake and OnEnable functions for all scripts will be called before Start, Update, etc are called for any of them. Naturally, this cannot be enforced when an object is instantiated during gameplay." and of course instantiating post-editor, during the loading of a save, is theoretically in gameplay. So I am asking about my specific case hoping that someone can address that.

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

141 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 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

How to be notified of new GamObject creation? 1 Answer

Why does my instatiate crashes editor? 1 Answer

How to make script affect only one this game object 2 Answers

Change position of instantiated parent's children 1 Answer

Instantiated Bullets Not Picking Up Correct Position/Rotation 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