Legacy Documentation: Version 5.2
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

Sumbission failed

For some reason your suggested change could not be submitted. Please try again 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 function Find(name: string): GameObject;
public static GameObject Find(string name);

Parameters

Description

Finds a game object by name and returns it.

If no game object with 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("Monster/Arm/Hand");
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public GameObject hand; void Example() { hand = GameObject.Find("Hand"); hand = GameObject.Find("/Hand"); hand = GameObject.Find("/Monster/Arm/Hand"); hand = GameObject.Find("Monster/Arm/Hand"); } }

This function is most useful to automatically connect references to other objects at load time, eg. inside MonoBehaviour.Awake or MonoBehaviour.Start. You should avoid calling this function every frame eg. MonoBehaviour.Update for performance reasons. A common pattern is to assign a game object to a variable inside MonoBehaviour.Start. And use the variable in MonoBehaviour.Update.

	// 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 ExampleClass : MonoBehaviour { private GameObject hand; void Start() { hand = GameObject.Find("/Monster/Arm/Hand"); } void Update() { hand.transform.Rotate(0, 100 * Time.deltaTime, 0); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961