GameObject.GetComponent Manual     Reference     Scripting  
Scripting > Runtime Classes > GameObject
GameObject.GetComponent

function GetComponent (type : Type) : Component

Description

Returns the component of Type type if the game object has one attached, null if it doesn't. You can access both builtin components or scripts with this function.

GetComponent is the primary way of accessing other components. From javascript the type of a script is always the name of the script as seen in the project view. Example:

function Start ()
{
var curTransform : Transform;

curTransform = gameObject.GetComponent(Transform);
// This is equivalent to:
curTransform = gameObject.transform;
}
function Update ()
{
// To access public variables and functions
// in another script attached to the same game object.
// (ScriptName is the name of the javascript file)
var other : ScriptName = gameObject.GetComponent(ScriptName);
// Call the function DoSomething on the script
other.DoSomething ();
// set another variable in the the other script instance
other.someVariable = 5;
}

For C# users there is a generic version available.

public class Something : MonoBehaviour
{
void Update()
{
//notice you don't need to cast here.
ScriptName sn = gameObject.GetComponent<ScriptName>()
sn.DoSomething();
sn.someVariable=5;
}
}

function GetComponent (type : string) : Component

Description

Returns the component with name type if the game object has one attached, null if it doesn't.

It is better to use GetComponent with a Type instead of a string for performance reasons. Sometimes you might not be able to get to the type however, for example when trying to access a C# script from Javascript. In that case you can simply access the component by name instead of type. Example:

function Update ()
{
// To access public variables and functions
// in another script attached to the same game object.
// (ScriptName is the name of the javascript file)
var other = gameObject.GetComponent("ScriptName");
// Call the function DoSomething on the script
other.DoSomething ();
// set another variable in the the other script instance
other.someVariable = 5;
}