Here’s my original reply translated (and in a few cases - updated) to UnityScript:
I think the first thing to understand is the difference between a GameObject and a Component.
http://unity3d.com/learn/tutorials/modules/beginner/editor/game-objects-and-components?playlist=17090
The GameObject is the THING that holds all of the Components. The Components are the little detailed containers that defines what the GameObject does. Components are created when you add a script to a GameObject. The Transform is a Component. On a light, the Light is a Component. If you write a custom class, say PlayerController.cs; when you add this script to a game object, it creates a Component on the GameObject.
In your code: private var et_player: ET_Player = GetComponent(ET_Player); ET_Player is a Script that defines a Type and creates a Component on a GameObject when it’s attached.
Get Component finds a component by Type (which is the name of the class and name of the script) on the GameObject that is referenced in the GetComponent call. If no GameObject is specified, the call assumes you mean on the GameObject that the script/component is attached to.
When you say:
var myLorem : LoremIpsum = GetComponent.();
… you are expecting to find an instance of LoremIpsum on THIS GameObject, as there is no additional GameObject reference.
This is great if you have, say, a PlayerController script that needs to access the Rigidbody or AudioComponent on that same Player GameObject. Simply use:
var rb : Rigidbody = GetComponent.();
… and then later in your code, use:
rb.AddForce(someValue);.
Now, if you need a component on ANOTHER GameObject, then you need a reference to that GameObject first.
HOWEVER - you may just want a reference to the COMPONENT itself.
One way of doing this is by associating your other GameObject and its Component with a variable that can be seen in the Inspector.
public var myLorem : LoremIpsum; works.
If you want to have the variable private but need to access it in the Inspector, then try:
[SerializeField]
private var myLorem : LoremIpsum;
Then you can drag the GameObject you want to reference into the slot.
Unity is smart and will find the correct Component on the GameObject to match the Type (in this case our made up LoremIpsum type) of the Component with the Field reference Type.
If you must find the component at runtime, then you must find the GameObject first. You can do a GameObject.Find, but I would suggest trying to use GameObject.FindByTag() or one of the other finds. To find more information on the GameObject find calls, please check the documentation and be aware that they are slow, so you should cache the reference in Start.
Once you have found the GameObject that has the Component you want, you can then use
theGameObjectFound.GetComponent.(); to get the Component from that GameObject.
You can see this in Trigger events, like “OnTriggerEnter”. The trigger event callback will give you a reference to the other Collider you hit. With the reference to the collider, you can get a reference to the GameObject. With the reference to the GameObject, you can get the Component:
function OnTriggerEnter(other : Collider) {
LoremIpsum myLorem = other.gameObject.GetComponent<LoremIpsum>();
myLorem.SomeFunction(someValue);
}
Does this make sense?