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?
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.
yoyo
5
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.
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.
Here is an additional link from the Unity Manual
Peter.
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
Vionix
8
Anyone struggling to understand the difference can take a look at this. Tested in Unity Editor