GetComponent(); in Awake cant be accessed in Update

Hi.

I dont know if I am doing something wrong but, even with scripts not written by me I cant use a variable(var) using GetComponent from a different function than I am referring it from(Does that make sense)? I will give you an example:

function Awake (){
var controller : CharacterController = GetComponent(CharacterController); //Do it in awake instead of Update to preserve performance.
}

function Update (){
if(controller.isGrounded){
    print("We are grounded");
    } 
}

But it will say “Unknown identifier ´controller´” Or something like that.

But if I use GetComponent in Update it will compile just fine. But for performance reasons I want to do it in awake.

So am I doing anything wrong?

Thank you. :slight_smile:

Variables only exist in there own ´scope´. Scope is a block of code, create by { }.
Variables declared outside of any function live in the class, which in JS is the entire script. Variables declared inside a function only live inside that function.

This is extremely basic stuff. If you don’t know this I advice you to read a book for follow a few guides.

var globalVar : boolean;

function Start()
{
    var inFunctionVar: boolean;
}

function Update()
{
    //globalVar can be accessed here, inFunctionVardoes not exist here.
}