How to reference GameObject in Awake or Start

When I try to assign a component variable in Start or Awake and then reference it in Update
I get an error:

function Start () { 
	var player = GameObject.FindGameObjectWithTag("Player"); 
	var freeMovementMotor = player.GetComponent(FreeMovementMotor);
}

function Update () { 
    if (Input.GetMouseButtonDown (1)) {
    freeMovementMotor.walkingSpeed += 4;  
    }
}

Error: Unknown identifier: ‘freeMovementMotor’

If I declare the component in the Update it works just fine:

function Update () { 
	var player = GameObject.FindGameObjectWithTag("Player"); 
	var freeMovementMotor = player.GetComponent(FreeMovementMotor);
    if (Input.GetMouseButtonDown (1)) {
    freeMovementMotor.walkingSpeed += 4;  
    }
}

But I don’t want to Find the component every second…

You have to declare the variable outsde of Start or Update so it becomes a member variable of your script. You can assign a reference to the variable in Start and use it in Update.

var player : GameObject;
var freeMovementMotor : FreeMovementMotor;

function Start ()
{
    player = GameObject.FindGameObjectWithTag("Player");
    freeMovementMotor = player.GetComponent(FreeMovementMotor);
}
 
function Update ()
{
    if (Input.GetMouseButtonDown (1))
    {
        freeMovementMotor.walkingSpeed += 4;
    }
}

In case anyone else has this problem…

I couldn’t get assigning gameobjects by Tag in the Start/Awake function and then referencing it in the Update to work… ever.
I tried again and again and always received errors when trying to reference the declared and already assigned variable within Update.

The only way to get it to work is to assign the variable again and again on every Update, but that sucks.

Maybe I’m doing something wrong, i don’t know, but I’m tired of testing it. Every time I think… “hmm maybe I was doing something wrong that one time”… “let me do as the docs say, it will be different this time”… “it will work this time”… NOOOO!

I’m convinced it’s a Unity error.
so… moving on

What I have found to work, so far on every occasion, is referencing the Gameobject in Start via the object’s name rather than it’s tag .

Using the above code as example:

This Works:

function Start () { 
    player = GameObject.Find ("Player"); 
    freeMovementMotor = player.GetComponent(FreeMovementMotor);
}

Instead of this, that Doesn’t Work:

function Start () { 
    player = GameObject.FindGameObjectWithTag("Player"); 
    freeMovementMotor = player.GetComponent(FreeMovementMotor);
}

Note: don’t forget to declare it before start:

var player : GameObject;

Or you can declare it in start, whatever:

var player : GameObject = GameObject.Find ("Player");