Unity 3D: Method to get inactive game objects

1. Get the activated game object

1. Gameobject.Find ("Game Object Name") // Get the game object directly according to the object name

  This method can find the specified object, but there are some defects. First, if there is a duplicate name in the scene, this method finds the object whose first name appears in Hierarchy from top to bottom. Second, if the activeSelf of the object is false, then this method can never find the object.

1.1. Methods to solve the above first problem (duplicate name):

  Gameobject.Find ("Parent / Son / Game Object Name") // Find by path, separated by /

1.2. Solve the second problem:

  Transform.Find ("Game Object Name")  

  This method can find the position of the object, regardless of whether the object is activated or not

  You can first find the root node of the object, and then use the method in the root node (this method can only find child nodes):

  GameObject root = GameObject.Find ("root node");

  GameObject  son  =   root.transform.Find("对象名").gameObject;

  If there is a parent node under the root node, it can also be found by using the path.

 

 

Second, find objects through tag tags

  GameObject>FindGameObjectsWithTag("tag")

  GameObject.FindWithTag("tag")

 

 

 

Refer to the article from yusongmomo, thanks!

Published 16 original articles · praised 7 · 20,000+ views

Guess you like

Origin blog.csdn.net/yuecangjiao5151/article/details/74906549