Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Kevin_C · Jun 11, 2012 at 05:01 PM · instantiateawakeorder-of-execution

instatiation order of events

I am building a game that does on the fly creation of the world at the beginning of each level (I am living with the lag time of creating, and destroying objects on the fly, and since it is for PC it is acceptable). I have prefabs that have children with scripts that will instantiate other GameObjects in their Awake() (work around for nested prefabs, and some functionality), and then logs that gameObject for referencing/tracking/bookKeeping.

If I have a script classA that Instantiates an object thingB at lineX will the scripts attached to thingB have their Awake/constructor methods called before lineX+1 of classA is processed so that I can access members of those scripts attached to thingB, or will I have to do that in a different place (like having all of these things created in classA Awake, and then start accessing those members in classA 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rutter · Jun 11, 2012 at 06:00 PM

When in doubt, test. It should be pretty straightforward to write a pair of classes:

  • TestChild logs its Start() and Awake() calls.

  • TestParent logs messages before and after creating a TestChild instance.

  • Attach a TestParent in your scene, and observe the logging order.

Most important: is the order of events reliable?

If you absolutely need a particular object to initialize immediately, why not write some code to make sure that happens? There are a few ways you could accomplish that:

  • Use some "lazy initialization" pattern, so that the object always initializes itself the first time anything accesses its data members.

  • Expose a function in your child class, such that the parent can immediately call it when creating its child.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Referencing instantiated player in camera's Start() 2 Answers

When is Awake called on an inactive gameobject 3 Answers

unity3d can't create instantiated reference in awake 1 Answer

Instantiate with name that can be used in Awake 1 Answer

Defender 2d Shooter - Loading Enemies (waves of enemies) 1 Answer


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