9. Common script types
Whenever a new Javascript is created, by default it contains an Update() function. This
section will discuss other common options available, simply replace the name of the
Update() function with one from the list below.
FixedUpdate()
Code placed inside this function is executed at regular intervals (a fixed framerate). It is
common to use this function type when applying forces to a Rigidbody.
// Apply a upwards force to the rigid body every frame
function FixedUpdate () {
rigidbody.AddForce (Vector3.up);
}
Awake()
Code inside here is called when the script is initialized.
Start()
This is called before any Update() function, but after Awake(). The difference between the
Start () and Awake() functions is that the Start() function is only called if the script is
enabled (if its checkbox is enabled in the Inspector View).
OnCollisionEnter()
Code inside here is executed when the game object the script belongs to collides with
another game object.
OnMouseDown()
Code inside here is executed when the mouse hovers over a game object which contains
a GUIElement or a Collider.
// Loads the level named "SomeLevel" as a response
// to the user clicking on the object
function OnMouseDown () {
Application.LoadLevel ("SomeLevel");
}
9
OnMouseOver()
Code inside here is executed when the mouse hovers over a game object which contains
a GUIElement or a Collider.
// Fades the red component of the material to zero
// while the mouse is over the mesh
function OnMouseOver () {
renderer.material.color.r -= 0.1 * Time.deltaTime;
}
Check the Unity API for more information on all of these functions.
Summary
This tutorial has introduced the essential scripting concepts in Unity. You
should now read other Unity tutorials or try experimenting yourself!

