- Home /
What is the general use of Awake(), Start(), Update(), FixedUpdate(), LateUpdate()?
Can you please provide me with some general usage examples of the following methods in the MonoBehaviour class.
For Initiating:
Awake()
Start()
- Why do we have two initiation methods?
- What are the differences?
- What are the best practices when using them?
For Updating:
Update()
FixedUpdate()
LateUpdate()
- Why three different update methods?
- What are the differences?
- ... and again some best practices?
If need more explanation you can refer to http://www.unityrealm.com/monobehaviour-unity-basic-tips/
Answer by runevision · Jan 15, 2010 at 09:29 AM
This really is something that is well covered in the scripting reference, so I'll just link to the descriptions:
Quoting from the docs:
The difference between Awake and Start is that Start is only called if the script instance is enabled. This allows you to delay any initialization code, until it is really needed. Awake is always called before any Start functions. This allows you to order initialization of scripts.
LateUpdate is called after all Update functions have been called. This is useful to order script execution. For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.
Also note that LateUpdate is called after animations are applied - this means you can implement procedural animation in LateUpdate which modifies the pose sampled by the animation system.
FixedUpdate is called every fixed framerate frame, if the MonoBehaviour is enabled. FixedUpdate should be used instead of Update when dealing with Rigidbody. For example when adding a force to a rigidbody, you have to apply the force every fixed frame inside FixedUpdate instead of every frame inside Update.
I hope that helps. You can read more about overridable functions on the scripting reference page for MonoBehaviour.
You can also read here about the Update Order.
I finally understand the difference. Thank you for putting it together so well in one post!
Update Order link has changed since the time of this writing. It is now here (http://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html) and includes the initialization and render steps as well.
Use the link above ... ^^ jedd's is the best answer. I can't vote it up enough times to get it to peoples attention :(
Thank you runevision for your help pointing the main differences between awake and start and it is good that jedd.ahyoung updated the link!
Answer by yoyo · Jan 31, 2011 at 04:32 PM
Regarding Awake and Start, I treat Awake as "initialize me" and Start as "initialize my connections to others." You can't rely on the state of other objects during Awake, since they may not have Awoken themselves yet, so it's not safe to call their methods. Once you get to Start, all objects are Awake and can call each other.
Very interesting concept. I'm glad I caught this now, before I ended up with a mass of pointless bugs later. Not repped enough to thumbs up, but I totally would.
I was thinking which one to use awake or start for initialization, you cleared the doubt. Thanks a lot.
Answer by SisterKy · Jan 15, 2010 at 01:08 PM
Also I'd like to add this quickstart-overview form the forums
http://forum.unity3d.com/viewtopic.php?t=2719 ( also read the rest of the thread! )
These go some deeper about execution-order:
the Wiki-page http://www.unifycommunity.com/wiki/index.php?title=Event_Execution_Order
http://answers.unity3d.com/questions/1405/is-function-update-short-for-function-start-whiletrue
(example of co-routines fitting between Update and Late-Update as Mr. Ante described in the forum-post above)
http://answers.unity3d.com/questions/1254/is-invoke-just-a-short-version-for-yield-waitforseconds-functioncall
(executionorder of invoke vs. co-routine)
Greetz, Ky.
Answer by theRiley · Sep 06, 2014 at 10:19 PM
if anyone else stumbles into this, all the info above was helpful, but I also liked the examples in this video
http://unity3d.com/learn/tutorials/modules/beginner/scripting/awake-and-start