- Home /
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)?
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 itsStart()
andAwake()
calls.TestParent
logs messages before and after creating aTestChild
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.
Your answer
Follow this Question
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