This is the sixth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Preface:

A small white can also understand the way to find the game object parsing – Unity’s several ways to find objects (two). This article through the actual test to draw the conclusion of use, we have a simple record, in the use of can not remember to see, use a few times, there is no problem.

A, the Transform. The Find ()

Find mount object parent, sibling, child objects:

void TransformFind()
{
    / / find the parent
    Transform parent = transform.Find("Root");
    Debug.Log("Find the parent object, find it or not:"+ (parent ! =null));
    
    / / to find at the corresponding levels
    Transform selfObj = transform.Find("Parent_1");
    Debug.Log("Look for objects of the same level, find or not:"+ (selfObj ! =null));
    
    / / for children
    Transform child = transform.Find("Child");
    Debug.Log("Looking for sublevel objects, find or not:"+ (child ! =null));
    
    // Find the child hidden object
    Transform childHide = transform.Find("ChildHide");
    Debug.Log("Find child hidden object, found or not:"+ (childHide ! =null));
}
Copy the code


Find multilevel subobjects:

// Find the second order subobject
Transform child_1 = transform.Find("Child_1_1");
Debug.Log("Find second-level subobject parameters write name only, whether find:"+ (child_1 ! =null));
// Find the second order subobject
Transform child_1_1 = transform.Find("Child/Child_1_1");
Debug.Log(Write the full path to the secondary subobject parameter.+ (child_1_1 ! =null));
Copy the code

Find() draws the conclusion that:

  1. You can only look for its children, not its peers or higher
  2. Finding child objects without considering whether they are disabled (hidden)
  3. Write full path when finding multilayer subobjects (otherwise, they will not be found even if they exist)

Second, the Transform. FindObjectOfType ()

After testing and GameObject. FindObjectOfType () makes no difference, test results, the test code and the screenshot is not hair processing take a place.

Type test I found GameObject. FindObjectsOfType < > (), and the Transform. FindObjectsOfType < Transform > () be merged, it should be said is a complete method, according to the picture below you can see, Although I been playing before the Transform of labels, but it is gray, the mouse on see method reference is a GameObject. FindObjectsOfType.

Conclusion: Transform. FindObjectOfType () and GameObject. FindObjectOfType () use, the results also no difference…


Three, the transform GetChild ()

Transform.getchild () is a method for finding child objects, and it’s my personal favorite. The downside is that you can’t change the hierarchy of game objects at will.

It’s also very simple to use: find the first object of a subobject of the first order

Transform child1 = transform.GetChild(0);
Copy the code

Find the third child of the first child of a first order child

Transform child1 = transform.GetChild(0).GetChild(2);
Copy the code

How to use GetChild() : several levels, the parameter is the current level of the object (starting from 0)


Use extension:

  • Traversing subobjects:
for (int i = 0; i < transform.childCount; i++)
{
     Debug.Log(transform.transform);
}
Copy the code
  • Gets the parent of the current object transform.parent

  • Gets the root object transform.root of the current object


Transform.getchild ()

  • Look for subobjects based on themselves. (Note that the index starts at 0.
  • You can usetransform.parent.parentAnd then it goes up and up, and then it goes upGetChild(), the purpose of finding objects at parent level or higher level is achieved
  • The downside is that it relies on the hierarchies of game objects, which need to be relatively stable when used. If instability causes the code to have to be changed every time the game body is changed, it adds to the workload.

That’s all for this article. Thank you for watching. If it is helpful to you, I hope I can support you three times. If you don’t have a solution or a better solution, please leave a comment.

How to find game objects in Unity: juejin.cn/editor/draf…