Scripting Overview

This is a short overview of how scripting inside Unity works.

Scripting inside Unity consists of attaching custom script objects called behaviours to game objects. Different functions inside the script objects are called on certain events. The most used ones being the following:

Update:
This function is called before rendering a frame. This is where most game behaviour code goes, except physics code.
FixedUpdate:
This function is called once every physics time step. This is the place to do physics-based game behaviour.
Code outside any function:
Code outside functions is run when the object is loaded. This can be used to initialise the state of the script.

Note: Sections of this document assume you are using Javascript, but see Writing scripts in C# & Boo for information about how to use C# or Boo scripts.

You can also define event handlers. These all have names starting with On, (i.e. OnCollisionEnter). To see the full list of predefined events, see the documentation for MonoBehaviour.

Subsections

Common Operations

Keeping Track of Time

Accessing Other Components

Accessing Other Game Objects

Vectors

Member Variables & Global Variables

Instantiate

Coroutines & Yield

Writing Scripts in C# & Boo

The most important classes

Performance Optimization

Script compilation (Advanced)

Unity and Mono compatibility

Generic Functions