Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
33
Question by runevision · Jan 21, 2010 at 10:17 AM · transformchildparenting

How can I access the children of a Transform?

I can get the parent of a Transform by using transform.parent, but how do I get the children?

I can use transform.childCount to get the number of children, but how can I access them?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
97
Best Answer

Answer by duck · Jan 21, 2010 at 10:38 AM

Here is the code in both Javascript and C# for iterating through each of the immediate children of your gameObject's transform:

Javascript:

for (var child : Transform in transform) {
    // do whatever you want with child transform here
}

C#:

foreach (Transform child in transform)
{
    // do whatever you want with child transform object here
}

However, the above code will only give you the transform's immediate children. If your transform has a number of levels of hierarchy beneath it, you may want to access every object beneath it. In this case, you can use "GetComponentsInChildren", like this:

Javascript:

var allChildren = gameObject.GetComponentsInChildren(Transform);
for (var child : Transform in allChildren) {
    // do whatever with child transform here
}

C#:

Transform[] allChildren = GetComponentsInChildren<Transform>();
foreach (Transform child in allChildren) {
    // do whatever with child transform here
}

Note however, that results from GetComponentsInChildren will also include that component from the object itself. So the name is slightly misleading - it should be thought of as "Get Components from Self And Children"!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MarkD · Dec 10, 2013 at 10:06 PM 0
Share

Already did that few $$anonymous$$ ago, just came back to delete this one.

avatar image Bunny83 · Dec 10, 2013 at 10:06 PM 1
Share

@$$anonymous$$arkD:
You probably want to set child.localPosition. child.position sets a world position which gets translated into the local position behind the scene.

ps: You should also read ducks post carefully. GetComponentsInChildren will also include the Transform of the parent object as well, also keep in $$anonymous$$d that you iterate over all Transform components which are a child or a child of a child. It iterates over the whole hierarchy downwards.

Usually the first solution in ducks post is the preferred one. GetComponentsInChildren is only used in special cases.

avatar image eelstork · Mar 18, 2015 at 04:06 AM 0
Share

The javascript version will issue a warning for implicit downcast. Is there an elegant way to solve this?

avatar image
6

Answer by Mark-Davis · Aug 08, 2011 at 07:33 AM

This C# class will extend all GameObjects to allow easy walking of an entire transform tree. It's inclusive, so it hits the root first, and then all the children, as deep as they go. If this looks strange, or too good to be true, google C# extension methods.

Example usage for any GameObject in any other script.

 myGameObject.VfxWalk(o => o.hideFlags = HideFlags.HideAndDontSave);

or another example with more robust syntax:

 myGameObject.VfxWalk((o) => { Debug.Log(o.name); });


 public static class GameObjectUtils
 {
     public static void VfxWalk(this GameObject o, System.Action<GameObject> f)
     {
         f(o);
 
         int numChildren = o.transform.childCount;
 
         for (int i = 0; i < numChildren; ++i)
         {
             o.transform.GetChild(i).gameObject.VfxWalk(f);
         }
     }
 }


voxelform.com

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aboveyou00 · Apr 16, 2013 at 03:23 AM 0
Share

I'm glad to see I'm not the only one who does this :D

avatar image twobob · Apr 12, 2015 at 12:00 AM 0
Share

+1, brief nod to zero enumeration cost! I like this way best. (linq examples aside) :)

avatar image superventure · Apr 19, 2015 at 10:12 PM 0
Share

Thank you for thinking out of the box- Awesome

avatar image Zengon · Dec 01, 2019 at 02:15 PM 0
Share

Exactly what I was looking for, thank you! I literally just wanted ti figure out how to get the child count, so many people overcomplicating things!

avatar image
4

Answer by runevision · Jan 21, 2010 at 10:19 AM

The first piece of example code in the scripting reference for the Transform class shows how to do this:

// Moves all transform children 10 units upwards!
for (var child : Transform in transform) {
    child.position += Vector3.up * 10.0;
}

If you want not only the immediate children but also the grandchildren etc., you can use GetComponentsInChildren:

// Rotate all transform children (and their children, etc.)
// by 90 degrees relative to their parents.
var children = GetComponentsInChildren (Transform);
for (var child : Transform in children) {
    child.Rotate(0, 90, 0);
}
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dissidently · Dec 20, 2010 at 12:33 PM 0
Share

How do you do this selectively, to only one child, say nested two deep, so it's a grandchild of the object with the script attached.

avatar image runevision ♦♦ · Dec 21, 2010 at 10:34 AM 0
Share

You can find the "first" child by putting a break; inside the loop - however, it is undefined what the first child is, so this is only reliable if there is only one child. You can find the child of a child by using a nested loop and putting a break; in both the inner and outer loop.

avatar image yoyo · Jan 31, 2011 at 06:17 AM 0
Share

dissidently, there's nothing like xpath for X$$anonymous$$L, if that's what you're looking for. (Though that would be handy -- let us know if you implement it!)

avatar image
0

Answer by Ippokratis · May 27, 2011 at 02:30 PM

Try this if other Unity Script solutions fail (as in my case, Unity 3.3 ):

 function Start()
 {
     var myTrans:Transform[]  = gameObject.GetComponentsInChildren.<Transform>() as Transform[];
 
     for (var child : Transform in myTrans ) 
     {
         child.position  = Vector3 (1.0,1.0,1.0);
     }
         
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by jhon143 · Jan 15, 2021 at 05:34 AM

This is how we can get the child's count of each parent

public List Bottles = new List();

foreach(GameObject GetBottles in Bottles) { Debug.Log("Get Bottles are : " + GetBottles); if (GetBottles.transform.childCount > 1) { Debug.Log(GetBottles + "has Child Count is "+GetBottles.transform.childCount.ToString()); } }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawning Children and Destroying them. 1 Answer

transform.Find(string)? 2 Answers

Parenting Transforms, Must (still) Set Parent at 0,0,0 ? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

a child object that inherits only position from parent 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges