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.
function Update () { var otherScript: OtherScript = GetComponent(OtherScript); otherScript.DoSomething(); }
using UnityEngine; using System.Collections;
public class example : MonoBehaviour { void Update() { OtherScript otherScript = GetComponent<OtherScript>(); otherScript.DoSomething(); } }
import UnityEngine import System.Collections
class example(MonoBehaviour):
def Update(): otherScript as OtherScript = GetComponent[of OtherScript]() otherScript.DoSomething()
1. Through inspector assignable references.
You can assign variables to any object type through the inspector:
var target : Transform; function Update () { target.Translate(0, 1, 0); }
using UnityEngine; using System.Collections;
public class example : MonoBehaviour { public Transform target; void Update() { target.Translate(0, 1, 0); } }
import UnityEngine import System.Collections
class example(MonoBehaviour):
public target as Transform
def 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.
var target : OtherScript;
function Update () { target.foo = 2; target.DoSomething("Hello"); }
using UnityEngine; using System.Collections;
public class example : MonoBehaviour { public OtherScript target; void Update() { target.foo = 2; target.DoSomething("Hello"); } }
import UnityEngine import System.Collections
class example(MonoBehaviour):
public target as OtherScript
def Update(): target.foo = 2 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:
transform.Find("Hand").Translate(0, 1, 0);
using UnityEngine; using System.Collections;
public class example : MonoBehaviour { void Example() { transform.Find("Hand").Translate(0, 1, 0); } }
import UnityEngine import System.Collections
class example(MonoBehaviour):
def Example(): 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.
transform.Find("Hand").GetComponent(OtherScript).foo = 2;
transform.Find("Hand").GetComponent(OtherScript).DoSomething("Hello");
transform.Find("Hand").rigidbody.AddForce(0, 10, 0);
using UnityEngine; using System.Collections;
public class example : MonoBehaviour { void Example() { transform.Find("Hand").GetComponent<OtherScript>().foo = 2; transform.Find("Hand").GetComponent<OtherScript>().DoSomething("Hello"); transform.Find("Hand").rigidbody.AddForce(0, 10, 0); } }
import UnityEngine import System.Collections
class example(MonoBehaviour):
def Example(): transform.Find('Hand').GetComponent[of OtherScript]().foo = 2 transform.Find('Hand').GetComponent[of OtherScript]().DoSomething('Hello') transform.Find('Hand').rigidbody.AddForce(0, 10, 0)
You can loop over all children:
for (var child : Transform in transform) { child.Translate(0, 10, 0); }
using UnityEngine; using System.Collections;
public class example : MonoBehaviour { void Example() { foreach (Transform child in transform) { child.Translate(0, 10, 0); } } }
import UnityEngine import System.Collections
class example(MonoBehaviour):
def Example(): for child as Transform in transform: child.Translate(0, 10, 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 () { var go = GameObject.Find("SomeGuy"); go.transform.Translate(0, 1, 0);
var player = GameObject.FindWithTag("Player"); player.transform.Translate(0, 1, 0); }
using UnityEngine; using System.Collections;
public class example : MonoBehaviour { void Start() { GameObject go = GameObject.Find("SomeGuy"); go.transform.Translate(0, 1, 0); GameObject player = GameObject.FindWithTag("Player"); player.transform.Translate(0, 1, 0); } }
import UnityEngine import System.Collections
class example(MonoBehaviour):
def Start(): go as GameObject = GameObject.Find('SomeGuy') go.transform.Translate(0, 1, 0) player as GameObject = 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
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 (other.rigidbody) other.rigidbody.AddForce(0, 2, 0); }
using UnityEngine; using System.Collections;
public class example : MonoBehaviour { void OnTriggerStay(Collider other) { if (other.rigidbody) other.rigidbody.AddForce(0, 2, 0); } }
import UnityEngine import System.Collections
class example(MonoBehaviour):
def OnTriggerStay(other as Collider): 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 (other.GetComponent(OtherScript)) other.GetComponent(OtherScript).DoSomething(); }
using UnityEngine; using System.Collections;
public class example : MonoBehaviour { void OnTriggerStay(Collider other) { if (other.GetComponent<OtherScript>()) other.GetComponent<OtherScript>().DoSomething(); } }
import UnityEngine import System.Collections
class example(MonoBehaviour):
def OnTriggerStay(other as Collider): if other.GetComponent[of OtherScript](): other.GetComponent[of 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 () { var other : OtherScript = FindObjectOfType(OtherScript); other.DoSomething(); }
using UnityEngine; using System.Collections;
public class example : MonoBehaviour { void Start() { OtherScript other = FindObjectOfType(typeof(OtherScript)); other.DoSomething(); } }
import UnityEngine import System.Collections
class example(MonoBehaviour):
def Start(): other as OtherScript = FindObjectOfType(OtherScript) other.DoSomething()
|