• 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 Rafes · Jul 17, 2011 at 04:27 PM · startsendmessageframebroadcastmessage

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

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
2
Best Answer

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.

Comment
Add comment · Show 4 · 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 Bunny83 · Jul 17, 2011 at 06:30 PM 0
Share

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 ;)

avatar image sneftel · Jul 17, 2011 at 06:44 PM 0
Share

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.

avatar image sneftel · Jul 17, 2011 at 06:44 PM 0
Share

Of course, by "the internal constructor" you probably meant the function that does the constructing. I apologize for being pedantic. :)

avatar image Rafes · Jul 18, 2011 at 12:16 AM 0
Share

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Passing 'null' as parameter to SendMessage/BroadcastMessage 2 Answers

BroadcastMessage from one Object to another ? 1 Answer

Sending and Receiving Messages globally with BroadcastMessage / SendMessage 4 Answers

Initialising List array for use in a custom Editor 1 Answer

SendMessage and BroadcastMessage not getting received outside the script. 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges