if you have a gameobject that is part of a model , like a turret on a tank , how do i
do
GameObject m_turret = find(“turret”);
i want to do it via code and not drag and drop through the inspector
if you have a gameobject that is part of a model , like a turret on a tank , how do i
do
GameObject m_turret = find(“turret”);
i want to do it via code and not drag and drop through the inspector
Find works by looking for the objects in the hierarchy by name:
GameObject myObject = GameObject.Find("ObjectName");
GameObject myObject = GameObject.Find("ObjectName/ObjectChild");
It is probably better to define it and use it in the Start function, something like:
void Start ()
{
GameObject myObject1 = GameObject.Find("ObjectName");
GameObject myObject2 = gameObject.transform.GetChild(0).gameObject;
GameObject myObject3 = GameObject.FindGameObjectWithTag("GameObject Tag").gameObject;
}
Assuming that the turret is a child of the tank the second way can return a child based on the index number in GetChild(int)