- Home /
Awake called after I activate object, not after Instantiate... is it normal??
As it documented, Awake should be called just after I instantiate object from prefab, and Start - after i activate object (before first Update), right?
I instantiate a Fighter object from prefab. It contains an inactive children in it:
Fighter/Explosion - inactive at instantiation time, activated when Fighter dying.
Fighter/Bullet - a pool of Bullet objects, all of they inactive at beginning.
each Bullet has an inactive child Bullet/Explosion - it activated when bullet hits some obstacle.
So, for each objects in this hierarchy an Awake is called when I instantiate a Fighter object, and Start is called for every object just when I activate this object.
But, Awake for Fighter/Bullet/Explosion is called... after I activated a Bullet and Explosion (when this Explosion became active in hierarchy), not directly after instantiation of all Fighter objects structure.
Is this behavior normal?? Is there some documented cases when (and why) it really should be so?
Thanks in advance.
Answer by 767_2 · Nov 08, 2014 at 04:02 PM
If a GameObject is inactive during start up , Awake is not called until it is made active, or an Unity lifetime function in any script attached to it is called. (OnEnable, OnCollisionEnter)
very interesting, so why Awake for Fighter/Bullet and Fighter/Explosion was called directly after Instantiate? they were inactive at this moment too.
and what function can I use to react the creation time for any object (active or inactive) ?
does it exist some function that called directly after Instantiate for all objects, including inactive?
@hav_ngs_ru: No. The only "method" that is called on the object is the constructor but it might be called from the loading thread so you can't do anything with the Unity API there. It's best you just forget about the constructor.
Inactive means inactive. It's just like the object doesn't exist from Unity's event-systems point of view. However that doesn't stop you from calling your own method manually after Instantiate. If you used a reference to the attached script of the prefab when you used Instantiate, you can directly call any method on the new instance. If you used Transform, GameObject or any other component type you can call GetComponent to get your script and call your method.
You can simply create the object active, then disable it during Awake, or immediately after Instantiate.
Thanks a lot for clarifying, this was one of those things where my knowledge was very wrong.
Seems like the best way is to make all prefabs and it`s children active, and deactivate they after instantiate... I have some extra work because if this, but seems like thats no alternatives... Im really surprised that Unity doesnt have some constructor analog for setup any object at moment when it is created. I had hoped so :)
And thanks a lot again :)
Answer by hav_ngs_ru · Nov 23, 2014 at 01:51 PM
Nonetheless, the Unity Manual says that "However, Awake is called when the script object is initialised, regardless of whether or not the script is enabled. Start may not be called on the same frame as Awake if the script is not enabled at initialisation time."
Where is the truth?
PS through a little refactoring this question doesnt have practically value for me now, but I really want to understand :)
@hav-ngs-ru:
You confuse two different things: the active state of a GameObject and the enabled state of the script component. If the GameObject is deactivated it's like the object doesn't exist (from Unity's point of view. no rendering, no physics, ...).
If the script component is disabled but the GameObject is active, Awake will be called immediately. That's because the GameObject itself is an active participent of the scene. The rigidbody could emit OnCollision / OnTrigger messages or a Renderer could call OnBecameVisible, OnPostRender ...
The "enabled" state just deter$$anonymous$$es the calling of Update / LateUpdate / FixedUpdate / OnGUI and Start.
If the GameObject itself is deactivated Unity won't interact with the GameObject or any attached component at all.
So when you have disabled the component and deactivated the gameobject in a prefab, nothing will happen when you instantiate it. When you later activate the Gameobject only Awake will be called. And if you finally set enabled to true Start will be called.
However if you instantiate such a prefab and set enabled to true while the GameObject is still deactivated, nothing will happens. But when you activate the GameObject later Awake and Start will be called.
Well, thanks a lot for detailed explanation! I really was confused with this two things, but they are completely clear for me now.