Here I will be listing some of the important auto execute events used in Unity3D:
Start()
Start is called just before any of the Update methods is called the first time.
Start is only called once in the lifetime of the behaviour. 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.
// Initializes the target variable. // target is private and thus not editable in the inspector private var target : GameObject; function Start () { target = GameObject.FindWithTag ("Player"); }
Awake()
Awake is used to initialize any variables or game state before the game starts. Awake is called only once during the lifetime of the script instance. Awake is called after all objects are initialized so you can safely speak to other objects or query them using eg. GameObject.FindWithTag.
Each GameObject’s Awake is called in a random order between objects. Because of this, you should use Awake to set up references between scripts, and use Start to pass any information back and forth.
Awake is always called before any Start functions. This allows you to order initialization of scripts. Awake can not act as a coroutine.
private var target : GameObject; function Awake () { target = GameObject.FindWithTag ("Player"); }
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.
The following paragraph taken from the Unity forums by user yoyo
“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.”
Update()
Update is called every frame.
// Moves the object forward 1 meter a second function Update () { transform.Translate(0, 0, Time.deltaTime * 1); }
LateUpdate()
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.
// Moves the object forward 1 meter a second function LateUpdate () { transform.Translate(0, 0, Time.deltaTime * 1); }
FixedUpdate()
This function is called every fixed framerate frame.
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.
// Apply a upwards force to the rigid body every frame function FixedUpdate () { rigidbody.AddForce (Vector3.up); }
Leave A Comment