Overview: Accessing Other Game Objects
Scripting
Overview: Accessing Other Game Objects

Most advanced game code does not only manipulate a single object. The Unity scripting interface has various ways to find and access other game objects and components there-in. In the following we assume there is a script named OtherScript.js attached to game objects in the scene.

var foo = 5;
function DoSomething ( param : String) {
print(param + " with foo: " + foo);
}

1. Through inspector assignable references.

You can assign variables to any object type through the inspector:

// Translate the object dragged on the target slot
var target : Transform;
function Update () {
target.Translate(0, 1, 0);
}

You can also expose references to other objects to the inspector. Below you can drag a game object that contains the OtherScript on the target slot in the inspector.

// Set foo DoSomething on the target variable assigned in the inspector.
var target : OtherScript;
function Update () {
// Set foo variable of the target object
target.foo = 2;
// Call do something on the target
target.DoSomething("Hello");
}

2. Located through the object hierarchy.

You can find child and parent objects to an existing object through the Transform component of a game object:

// Find the child "Hand" of the game object
// we attached the script to
transform.Find("Hand").Translate(0, 1, 0);

Once you have found the transform in the hierarchy, you can use GetComponent to get to other scripts.

// Find the child named "Hand".
// On the OtherScript attached to it, set foo to 2.
transform.Find("Hand").GetComponent(OtherScript).foo = 2;

// Find the child named "Hand".
// Call DoSomething on the OtherScript attached to it.
transform.Find("Hand").GetComponent(OtherScript).DoSomething("Hello");

// Find the child named "Hand".
// Then apply a force to the rigidbody attached to the hand.
transform.Find("Hand").rigidbody.AddForce(0, 10, 0);

You can loop over all children:

// Moves all transform children 10 units upwards!
for (var child : Transform in transform) {
child.Translate(0, 1, 0);
}

See the documentation for the Transform class for further information.

3. Located by name or Tag.

You can search for game objects with certain tags using GameObject.FindWithTag and GameObject.FindGameObjectsWithTag. Use GameObject.Find to find a game object by name.

function Start ()
{
// By name
var go = GameObject.Find("SomeGuy");
go.transform.Translate(0, 1, 0);

// By tag
var player = GameObject.FindWithTag("Player");
player.transform.Translate(0, 1, 0);

}

You can use GetComponent on the result to get to any script or component on the found game object

function Start ()
{
// By name
var go = GameObject.Find("SomeGuy");
go.GetComponent(OtherScript).DoSomething();

// By tag
var player = GameObject.FindWithTag("Player");
player.GetComponent(OtherScript).DoSomething();
}

Some special objects like the main camera have shorts cuts using Camera.main.

4. Passed as parameters.

Some event messages contain detailed information on the event. For instance, trigger events pass the Collider component of the colliding object to the handler function.

OnTriggerStay gives us a reference to a collider. From the collider we can get to its attached rigidbody.

function OnTriggerStay( other : Collider ) {
// If the other collider also has a rigidbody
// apply a force to it!
if (other.rigidbody) {
other.rigidbody.AddForce(0, 2, 0);
}
}

Or we can get to any component attached to the same game object as the collider.

function OnTriggerStay( other : Collider ) {
// If the other collider has a OtherScript attached
// call DoSomething on it.
// Most of the time colliders won't have this script attached,
// so we need to check first to avoid null reference exceptions.
if (other.GetComponent(OtherScript)) {
other.GetComponent(OtherScript).DoSomething();
}
}

Note that by suffixing the other variable in the above example, you can access any component inside the colliding object.

5. All scripts of one Type

Find any object of one class or script name using Object.FindObjectsOfType or find the first object of one type using Object.FindObjectOfType.

function Start ()
{
// Find the OtherScript which is attached to any game object in the scene.
var other : OtherScript = FindObjectOfType(OtherScript);
other.DoSomething();
}