Legacy Documentation: Version 2018.1 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

GameObject.Find

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public static method Find(name: string): GameObject;
public static GameObject Find(string name);

Description

Finds a GameObject by name and returns it.

This function only returns active GameObjects. If no GameObject with name can be found, null is returned. If name contains a '/' character, it traverses the hierarchy like a path name.

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.

Note: If you wish to find a child GameObject, it is often easier to use Transform.Find.

Note: If the game is running with multiple scenes then Find will search in all of them.

#pragma strict
// This returns the GameObject named Hand in one of the Scenes.
public var hand: GameObject;
// This returns the GameObject named Hand.
hand = GameObject.Find("Hand");
// Hand must not have a parent in the Hierarchy view.
hand = GameObject.Find("/Hand");
// Monster must not have a parent in the Hierarchy view.
hand = GameObject.Find("/Monster/Arm/Hand");
// which is a child of Arm > Monster.
hand = GameObject.Find("Monster/Arm/Hand");
using UnityEngine;
using System.Collections;

// This returns the GameObject named Hand in one of the Scenes.

public class ExampleClass : MonoBehaviour { public GameObject hand;

void Example() { // This returns the GameObject named Hand. hand = GameObject.Find("Hand");

// This returns the GameObject named Hand. // Hand must not have a parent in the Hierarchy view. hand = GameObject.Find("/Hand");

// This returns the GameObject 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 returns the GameObject named Hand, // which is a child of Arm > Monster. hand = GameObject.Find("Monster/Arm/Hand"); } }

GameObject.Find is useful for automatically connecting references to other objects at load time; for example, inside MonoBehaviour.Awake or MonoBehaviour.Start.

For performance reasons, it is recommended to not use this function every frame.

A common pattern is to assign a GameObject to a variable inside MonoBehaviour.Start, and use the variable in MonoBehaviour.Update.

#pragma strict
// Find the GameObject named Hand 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;

// Find the GameObject named Hand and rotate it every frame

public class ExampleClass : MonoBehaviour { private GameObject hand;

void Start() { hand = GameObject.Find("/Monster/Arm/Hand"); }

void Update() { hand.transform.Rotate(0, 100 * Time.deltaTime, 0); } }
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答