GetComponent Keeps Returning : Null??

Hello,

I have 2 scripts in my game, one that changes a variable true or false depending if the user has clicked on the object which has the script attached to it. And another that has a GetComponent to read if that variable is true or false and executes an if statement.

ClickObject:

#pragma strict

var clicked : boolean = false;
var hit : RaycastHit;
static var ClickedOn : boolean = false;
var ClickTurret = false;

function Update() {
    if(Input.GetMouseButtonDown(0) &&
       collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, Mathf.Infinity)) {
        clicked = !clicked;
        if(tag == "Select") tag = "Unselect";
        else tag = "Select";
        //Debug.Log("clicked" + (clicked? "" : " off"));
    }
ClickedOn = clicked;
ClickTurret = ClickedOn;
}

Other Script:

function Update () 
{
    var clickObject : ClickObject;
    clickObject = GetComponent(ClickObject);

    if(clickObject.ClickTurret == true) //clickObject.ClickedOn
    { ......

The problem is, is that the GetComponent keeps returning null. I get a NullReferenceExeption in my console: Object reference not set to an instance of an object?

Please Help, Thanks!

PS: I'm trying to avoid static / global variables.

The way you have it written, both scripts need to be on the same object. When you use GetComponent by itself, it only looks on the same game object, so you'll either need to find a reference to the game object with clickobject or use a static.

To reference a script on another object you could do something like this:

var myOtherScript: scriptName; 

myOtherScript = gameObject.FindWithTag("myOtherObjectTag").GetComponent(scriptName) as scriptName;

You can access it like:

myOtherScript.function();

or

myOtherScript.someVariable = something;

You could also search for the gameobject by name but it's recommended to use the FindWithTag way.

Edit: this seems more appropiate for your needs:

http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponentInChildren.html