- Home /
SendMessage is running before Start
I am instancing and object then sending a message to the new instance. I added some debug messages with Time.time and it appears that SendMessage runs before Start() in the same frame. This is a problem since Start() will often be used to initialize something. In my case, it is a something which I need to be available when SendMessage triggers a call.
Is this the expected behaviour or a bug?
Shouldn't Start() be the first call the frame after instanciation? The wiki doesn't have this information here: http://www.unifycommunity.com/wiki/index.php?title=Event_Execution_Order
Answer by sneftel · Jul 17, 2011 at 04:35 PM
Start()
is called on the next frame, but SendMessage()
is delivered immediately. Awake()
and OnEnable()
are called before Instantiate()
returns, so if you can sufficiently initialize the object there, do that.
Awake and OnEnable are called from the internal constructor. When Instantiate returns Awake has already been called so it's like the constructor of your class ;)
Hmmmm... I don't think they are called from a constructor. Looking at the stack trace from Awake()
invoked from Instantiate()
(the real stack trace, not the Unity-scrubbed one), it looks like Awake()
is directly invoked by UnityEngine.Object.INTERNAL_CALL_Internal_InstantiateSingle
, without an intervening constructor call.
Of course, by "the internal constructor" you probably meant the function that does the constructing. I apologize for being pedantic. :)
Thanks! Confirmation means I can move on! If you are a Pool$$anonymous$$anager user, please see this post for our work-around if this is a problem: http://poolmanager.freeforums.org/post23.html#p23 We basically added another event that can be used for init after Awake if needed, but is guaranteed to run right after Awake. I'm hoping this won't be necessary with the next release of Unity - I read it will allow us to explicitly set an order during Awake() if needed.
Your answer
Follow this Question
Related Questions
BroadcastMessage Functionality Help 1 Answer
Is it possible to use SendMessage() on an uninstantiated prefab? 0 Answers
What's the best way to get the total time since the start of the current frame? 1 Answer
Broadcast Message Problems 1 Answer
SendMessage getting "stuck" / looping animation, not returning to Update 1 Answer