How do I use GetComponent completely correct in this instance?

Okay so, I have a variable called Selector that holds the gameObject.GetComponent(“Selector”). Take a look at my code and I’ll tell you the issue afterwards.

 #pragma strict
    //variables
    var Selector : Selector;
    //variables
    
    function Awake(){
    	var Selector = gameObject.GetComponent("Selector");
    }

the variable Selector shows up in the inspector as if it’s typecasted as a GameObject. The thing is, I can ONLY select the current GameObject. It works, but it doesn’t seem as if it’s right. Do you guys know why this is happening? If this is too vague, I can go into more detail. It’s just kind of hard to explain.

First, you don’t want your variable to be the same name as the script/class. The usual convention is to make variables lower case, and functions and classes upper case. So line 3 would be:

var selector : Selector;

As for the GetComponent(), use the typed version, not the string version. So line 7 would be:

var selector = gameObject.GetComponent(Selector);

When you use the string version, the compiler does not know the type of the component, so it defaults to the base class of ‘Component’. You will would not be able to use ‘selector’ to access the functions and variables in the Selector component without casting, plus using the string version can hide bugs/problems that the compiler can catch.