unity search for gameobject by name code example
Example 1: get gameobject by name
using UnityEngine;
using System.Collections;
{
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");
}
}
Example 2: unity find gameobject by name
GameObject enemy = GameObject.Find("enemy");
Example 3: unity find game object by name
foreach(GameObject fooObj in GameObject.FindGameObjectsWithTag("foo"))
{
if(fooObj.name == "bar")
{
}
}
foreach (var obje in FindObjectsOfType(typeof(GameObject)) as GameObject[])
{
if(obje.name == "obje")
{
}
}
Example 4: unity gameobject.find
GameObject obj = GameObject.Find("/Parent/Child/ChildOfChild");