Unity3D basics - traversing child objects

Unity3D basics - traversing child objects


table of Contents

1, blog introduction

2, content

(1) traversing child objects

(2) traversing descendant objects

(3) Screening traversal

3, push

4, the conclusion


1, blog introduction

This article will record the basics of traversing sub-objects of an object or traversing sub-objects and grandchild objects, or filtering traversal


2, content

(1) traversing child objects

(This method traverses child objects, does not involve grandchild objects)

                foreach (Transform child in transform)
		{
			 Debug.Log("sub-object name:"+child.name);
		}
	        for (int i = 0; i < transform.childCount; i++)
		{
			var child = transform.GetChild(i);
			 Debug.Log("sub-object name:"+child.name);
		}

(2) traversing descendant objects

(This method can iterate through all child and grandchild objects)

                for (int i = 0; i < GetComponentsInChildren<Transform>(true).Length; i++)
		{
			 Debug.Log("Sub-object name:"+GetComponentsInChildren<Transform>(true)[i].name);
		}
                foreach(Transform child in GetComponentsInChildren<Transform>(true))
		{
			 Debug.Log("sub-object name:"+child.name);
		}

(3) Screening traversal

(This method can obtain objects with specific properties in descendant objects)

                for (int i = 0; i < GetComponentsInChildren<Button>(true).Length; i++)
		{
			 Debug.Log("Sub-object name:"+GetComponentsInChildren<Button>(true)[i].name);
		}

                                 Note: Output all Buttons in descendants
                foreach(Transform child in GetComponentsInChildren<Text>(true))
		{
			Debug.Log("sub-object name:"+child.name);
		}
                                 Note: Output all the Text in the descendant object

3, push

Nothing to push. . .


4, the conclusion

This record is used, and the blogger's ability is limited. If there is any error in the text, you are welcome to comment on the accusation.

QQ exchange group: 806091680 (Chinar)

This group is created by CSDN blogger Chinar, recommend it! I am also in the group!

  This article belongs to the original article, reproduced by the famous author source and top! ! ! ! !

Intelligent Recommendation

Unity3D: Remove all child objects under a component (clear list)

Unity3D Removes the child object under a component (list update) When updating the ScrollView list content, you usually need to empty the list, read data, and the empty list is actually destroying the...

Unity3D detached object cancels parent-child relationship / can also delete child objects

Regarding the dissolution of the father-son relationship, first look at the official explanation code. The official gives a function to remove the father-son relationship. Use the officially given fun...

Getting started with Unity3D: How to find the parent and child, grandchildren and their components of game objects in scripts

In real game scenes that can be played, many scripts are executed on uncertain game objects, so they will consider writing scripts on parent or child objects. At this time, you may need to find game o...

Solve the problem that the rotation of child objects in multi-level structures is affected by the size of the parent object in Unity3D

Preface In the process of using unity3D for scene design and object control, multi-layer object nesting is a structure that is often used. In multi-level objects, objects at each level may have child ...

More Recommendation

Javascript traversing arrays and objects

var arr = [18,20,26]; var obj = {name:"xiaohong",sex:"f",age:"18"}; for Can be used for traversing arrays for in/for of Traversing Note: for...of is suitable for traversi...

Js traversing json objects

Irregular: Regular (json array): You can also use this: Traversing the json object: There are the following json objects: Var obj ={"name": "Feng Juan", "password": "...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top