What's the difference between code in Awake() and code at the top of the script?

When setting up arrays, variables etc, I generally put setup code outside any function, at the top of the script alongside the variable declarations, instead of in Awake(). What's the difference?

Code outside of any function goes into a special separate function in Javascript (which is hidden from you, but incidentally is called "Main"). This function is called before Start, but not before Awake.

Awake is special in that it behaves similarly to a Constructor in that it is called immediately, and blocks the calling function (although technically it is not the constructor). This means if an object is instantiated at runtime, its Awake code will run straight away, and to completion, before the code which instantiated it is allowed to continue.

Start, on the other hand, is called later - usually after the entire current frame (including every current script's Update function) has finished processing. The hidden "Main" function containing your "code outside of functions" is called in a similar way, at a similar time - after the current frame has completed, but just before Start is called.

These small code snippets should illustrate this for you:

First, a script which allows us to instantiate objects:

var prefab : Transform;

function Update() {
    if (Input.GetMouseButtonDown(0)) {
        print("Before instantiating");
        Instantiate(prefab,Random.insideUnitSphere*10,Random.rotation);
        print("After instantiating");
    }
}

Next, a script placed on the prefab referenced above, which gets instantiated:

print("This is outside of any function, at the top");

function Awake () {
    print("This is Awake");
}

function Start () {
    print("This is Start");
}

function Update () {
    print("This is a frame");
}

print("This is outside of any function, at the bottom");

Now, the order of execution, as shown by our print statements:

  • Before instantiating
  • This is Awake
  • After instantiating
  • This is outside of any function, at the top
  • This is outside of any function, at the bottom
  • This is Start
  • This is a frame
  • This is a frame
  • This is a frame
  • (etc...)

The exception to this is where you declare variables outside of functions. For these, their definition - including any initial value assigned - occurs before Awake, so these values are available to use immediately from within any function.

Hope this helps clarify things!

There must be a ton of this question and a ton of answers but here you go:

Awake is called BEFORE start. Start is a place for your initialization:

private int x;
private Vector3 left;
private bool spelledRight;

void Start()
{
   x = 3;
   left = Vector3.left;
   spelledRight = true;
}

Awake is for checking if everything is there before the object finishes construction you need all the pieces.

private AnimationClip _ani;

Awake()
{
    if (GetComponent<AnimationClip>() != null)
    {
        _ani = GetComponent<AnimationClip>();
    }
}

There's some special functions aswell

// Mark the PlayerScript as requiring a rigidbody in the game object.
@script RequireComponent(Rigidbody)

function FixedUpdate() 
{
    rigidbody.AddForce(Vector3.up);
}

C# Example:

[RequireComponent (typeof (Rigidbody))]
class PlayerScript : MonoBehaviour 
{
    void FixedUpdate() 
    {
        rigidbody.AddForce(Vector3.up);
    }
}

PERSONAL RULE OF THUMB:

You check if it exists at all You assign whatever exists You don't EVER use anything untested or unchecked (Not double enough)

Fail any combination of the three and u risk serious crashing and headaches.

EDIT 11/16/2010 - 15:06

Oh and one more thing, please remember that putting everything of init in the Start is a bit of extra typing and may risk getting strange 0 values of you forget something. Why it's a great idea to do it all there? Well you may end multiclassing and/or multistructing. It keeps it all in a good logical spot. So is it worth it ? Up to you, just respect the Awake and look at start as a spot for inits.

I might just be totally missing the point of the Start method though, I have little evidence to back this up except Awake.

Code outside any function runs after Awake (but before Start).

Initializations, however, run before Awake if they're not in a function.

So you can have, for example,

var x : int = 7;
var y : float = 0.5;

function Awake() { print( x + "; " + y ); }

And that'll print "7; 0.5".

But if you use

var y : float = 0.5;
y += 1;

y won't be equal to 1.5 until after Awake.

There's no guarantee as far as which script runs first, just like Awake, but all objects will run their "outside" code after everything runs Awake.

Also, code outside functions does not fall out of scope, since every function is in a sub-block of the block the code is in.