MonoBehaviours, What are they?

If you have been used to working with JavaScript within Unity you will have already been using them, maybe without knowing. They are the base class that every script that can be attached to a GameObject within the editor.

The main functions that are used are Awake, Start, Update, FixedUpdate, LateUpdate and OnGUI, all are case sensitive. There are many, many more of course, feel free to find them here.

For information about how the calling order of MonoBehaviour functions are called please find this post from Unity

What do these functions do?

Awake()

The Awake function is called when the GameObject is attached to is set to an active state (or the scene loads and the object is already active). This allows you to initialize a script before it is enabled.
Example declaration

using UnityEngine;
using System.Collections;
public class Demonstration : MonoBehaviour {
	void Awake () {
		Debug.Log("Awake");
	}
}

[divider]

Start()

The Start function is called before the first Update method is called and it will only ever be called once. This is usually where the initialization of the script takes place (for instance, if you are going to define a reference to another GameObject at run-time). The Start function is never called before the Awake function and will only be called if the script is enabled.
The Start function can have either IEnumerator or void return type, think of this as either a asynchronous or synchronous call respectively.
[one_half]
Example declaration

using UnityEngine;
using System.Collections;
public class Demonstration : MonoBehaviour {
	void Start () {
	Debug.Log("Started");
	}
}

[/one_half][one_half_last]
Example declaration (Asynchronous)

using UnityEngine;
using System.Collections;
public class Demonstration : MonoBehaviour {
	IEnumerator Start () {
		Debug.Log("Started");
		yield return 0;
	}
}

[/one_half_last]
[divider]

Update()

This is where the bulk of your script will take place. This is called each frame before rendering and is only a synchronous function, make sure to keep the code light here otherwise you will risk performance issues.

Example declaration)

using UnityEngine;
using System.Collections;
public class Demonstration : MonoBehaviour {
	void Update() {
		Debug.Log("Update");
	}
}

FixedUpdate()

Another synchronous only function, that is called on the update of the physics engine within Unity. This means that it may or may not be called each frame (Possibly multiple times) and it depends on the tick-rate of the Time.fixedTime. This function is really useful if you are manipulating a Rigidbody at run-time (for instance adding a velocity).

Example declaration

using UnityEngine;
using System.Collections;
public class Demonstration : MonoBehaviour {
	void FixedUpdate() {
		Debug.Log("Fixed Update Demonstration");
	}
}

[divider]

OnGUI()

Fair warning on this one, Unitys native GUI system isn’t quite ready for commercial use yet. The reason being, each function to draw a GUI element currently causes another draw call to be used to render the UI element. If you are doing something to debug / prototype, feel free to use it but for a large-scale commercial project this will just cause you pain. I personally recommend 2D Toolkit, mainly because it is a generic sprite system that supports batching draw calls and can be used to make 2D games. It takes some work to get going as a GUI system but it can be done. I have also heard vey nice things about NGUI however as I haven’t tried it I wouldn’t know if its good or not.

OnGUI is a synchronous call that can end up being called multiple times in one frame, after the rendering pass. If you call any GUI methods within this function it will be drawn immediately to the screen. Contry to what you would expect, you do not have to have the MonoBehaviour attached to a camera to run GUI commands.

Example declaration

using UnityEngine;
using System.Collections;
public class Demonstration : MonoBehaviour {
	void OnGUI() {
		Debug.Log("GUI Demonstration");
	}
}

[divider]

LateUpdate()

This Synchronous function is called after all Update() functions have been called and can be used to handle situations where call order is important

Unitys example is if the Update function of another GameObject moves the object, and the script to control the camera follows that object. There are chances where the Update call of the camera might be behind the update call of the GameObject, so the LateUpdate call will gurantee that its always AFTER the Update function

Example declaration

using UnityEngine;
using System.Collections;
public class Demonstration : MonoBehaviour {
	void LateUpdate() {
		Debug.Log("LateUpdate Demonstration");
	}
}

[divider]

What next?

MonoBehaviour scripts are just the bedrock of Unity development, what you do from here is totally down to you. You could turn this simple idea into a spaceship in very little code, or perhaps a rifle? The power is in your imagination.

As I mentioned above, please go over the documentation to find out more of the calls that MonoBehaviours make. I have only listed the most common ones. For instance, did you know MonoBehaviours (if they have colliders attached) will call a function to let you know it was collided with? Or even if the GameObject it is attached to is currently being rendered.