Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Docs on calling order in MonoBehaviour

Discussion in 'Wish List' started by Jonathan Czeck, Aug 24, 2006.

  1. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Does OnTriggerEnter get called before or after Update? I'd like some documentation that discusses this and all the other functions in MonoBehaviour. If it is not appropriate to rely on the order of some of the functions, I'd like to know what orders I can rely on.

    -Jon
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Code (csharp):
    1.  
    2. * All Awake calls
    3.  
    4. * All Start Calls
    5.  
    6. while (stepping towards variable delta time)
    7. {
    8.     * All FixedUpdate functions
    9.     * Physics simulation
    10.     * OnEnter/Exit/Stay trigger functions
    11.     * OnEnter/Exit/Stay collision functions
    12. }
    13.  
    14. * Rigidbody interpolation applies transform.position and rotation
    15.  
    16. * OnMouseDown/OnMouseUp etc. events
    17.  
    18. * All Update functions
    19.  
    20. * Animations are advanced, blended and applied to transform
    21.  
    22. * All LateUpdate functions
    23.  
    24. * Rendering
    25.  
     
  3. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    That's very useful information, thanks! I added it to the wiki for reference.

    For clarity, could you explain what happens for scripts which are instantiated (via a prefab or AddComponent) while the game is running? I presume it calls Awake and Start in the usual way when the script appears, but is it guaranteed that no other event will fire on that script before those events are called?

    Also, I've been experimenting with GL functions in OnPostRender, and to test how things were coming out, I paused the in-progress game and kept editing and saving the script. This worked, but it appears that Start isn't called for scripts which are reloaded due to being recompiled during the game, so I had to do some initialisation in my OnPostRender method. Is this the expected behaviour?
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    In the case of Awake it is called during execution of the Instantiate function.

    OnEnable/OnDisable is always immediate.

    Start is delayed but always guaranteed to be before any Update or event function is called. And guaranteed before rendering.

    While we are on the topic. Destroy is always delayed.
    (Although you can use Destroy immediate, but that is almost NEVER what you want)


    All that said, there two important facts everyone should know and that are extremely useful when execution order matters:


    * All Awake functions are always before all Start functions
    * All Update functions are always before all LateUpdate functions
    [/b]
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You can override OnEnable for this instead.
     
  6. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Thanks a lot. :)

    Wow, I wasn't aware of OnDisable at all. I see that it fires when an object is destroyed, which is something I put in a feature request for some time ago! This is exactly what I need, and it'll help me to simplify a lot of my code.

    I feel like I often fail to notice when new features are added, even though I read the release notes. Would it be possible to have a 'what's new' section in the documentation?
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Where do coroutines fit in to all of this? For example, if you have a yield statement, when does the line after it start being executed?
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Bump, because I still need an answer to that question.
     
  9. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Right now coroutines are executed after all Update functions.
    And if you start a coroutine in LateUpdate it will also be called after LateUpdate just before rendering. I don't think you should rely on this though.
     
  10. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    How does OnLevelWasLoaded fit into the event execution order?
     
  11. llavigne

    llavigne

    Joined:
    Dec 27, 2007
    Posts:
    977
    So Animations are updated after physics? So a rigidbodied character's foot would go through the soccer ball ?
     
  12. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Not necessarily. There is a flag specifically for this situation. In the animation component click on animate physics.
     
  13. llavigne

    llavigne

    Joined:
    Dec 27, 2007
    Posts:
    977
    Thanks -

    With test, I found that OnEnable() is still called after awake() (wasn't sure what you meant by immediate)

    I am trying to debug a weird situation where onenable() in one script is being called twice before an awake() on another script, both attached to the same GO.

    Any idea ?
     
  14. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    Where does onGui fit into all this?