Finds a game object by name
and returns it.
name
can be found, null is returned.
If name
contains a '/' character it will traverse the hierarchy like a path name.
This function only returns active gameobjects.For performance reasons it is recommended to not use this function every frame
Instead cache the result in a member variable at startup or use GameObject.FindWithTag.var hand : GameObject; // This will return the game object named Hand in the scene. hand = GameObject.Find("Hand"); // This will return the game object named Hand. // Hand must not have a parent in the hierarchy view! hand = GameObject.Find("/Hand"); // This will return the game object named Hand, // which is a child of Arm -> Monster. // Monster must not have a parent in the hierarchy view! hand = GameObject.Find("Monster
Arm/Hand"); // This will return the game object named Hand, // which is a child of Arm -> Monster. // Monster may have a parent. hand = GameObject.Find("MonsterArm
Hand");
using UnityEngine; using System.Collections; public class Example : MonoBehaviour { public GameObject hand; void Example() { hand = GameObject.Find("Hand"); hand = GameObject.Find("/Hand"); hand = GameObject.Find("Monster
Arm/Hand"); hand = GameObject.Find("MonsterArm
Hand"); } }
import UnityEngine import System.Collections public class Example(MonoBehaviour): public hand as GameObject def Example() as void: hand = GameObject.Find('Hand') hand = GameObject.Find('/Hand') hand = GameObject.Find('Monster
Arm/Hand') hand = GameObject.Find('MonsterArm
Hand')
// Find the hand inside Start and rotate it every frame
private var hand : GameObject;
function Start () {
hand = GameObject.Find("Monster
Arm/Hand");
}
function Update () {
hand.transform.Rotate(0, 100 * Time.deltaTime, 0);
}
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
private GameObject hand;
void Start() {
hand = GameObject.Find("Monster
Arm/Hand");
}
void Update() {
hand.transform.Rotate(0, 100 * Time.deltaTime, 0);
}
}
import UnityEngine
import System.Collections
public class Example(MonoBehaviour):
private hand as GameObject
def Start() as void:
hand = GameObject.Find('Monster
Arm/Hand')
def Update() as void:
hand.transform.Rotate(0, (100 * Time.deltaTime), 0)