• 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
4
Question by bigbat · Jul 17, 2013 at 09:20 PM · transformgetcomponentdepthgetcomponentinchildren

How to get first depth child components of a parent object?

Hi to all.

If i have an gameobject that has more than one child gameobjects and each of it's child also have their child.Is it possible or is any way to get transform component of all first level child ? like below diagram where each of child repeat it's parent structure and i want access transform of child 1 until child n:

gameobject

child 1

child 2

.

.

child n

Comment
Add comment · Show 2
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 robertbu · Jul 17, 2013 at 09:42 PM 0
Share

Note the bit of source at the top of the file at this link:

http://docs.unity3d.com/Documentation/ScriptReference/Transform.html

avatar image bigbat · Jul 18, 2013 at 12:10 AM 0
Share

Thanks for reply

I already saw that link,but my question is how to get transform of child 1,child 2 ,..and child n ,and not transform of their child or all child transform of current gameobject(since child 1 ,..,child n themselves have child objects).

7 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by jedy · Feb 16, 2018 at 06:56 AM

The more performant solution is surprisingly the easier one - use GetComponentsInChildren and filter the results by transform.parent.


I tried two distinct approaches to test this:

  • Iterating child transforms and using GetComponent on every one of them (Method 1)

  • Using GetComponentsInChildren and checking the transform's parent of every component (Method 2)

I favored the first method (iterating the child transforms), but it turns out to be significantly slower. Unity caches GetComponents calls which greatly improves performance after the first call while the overhead of calling the underlying C++ code is more or less fixed.

With a single call/iteration in a 170 object hierarchy and 10 children, the first approach is better:

  • Method 1 - 160 ticks

  • Method 2 - 500 ticks

But with 5 calls, the results flip:

  • Method 1 - 368 ticks

  • Method 2 - 2422 ticks

Comment
Add comment · Show 1 · 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 ammirea · Jan 17, 2020 at 11:12 AM 0
Share

I'm missing something, are you sure the ticks are written correctly?

avatar image
0

Answer by qsleonard · Jul 21, 2020 at 08:29 AM

You can use LINQ:

    children = parent.GetComponentsInChildren<Transform>()
                     .Where(t => t.GetInstanceID() != parent.GetInstanceID() && t.parent == parent)
                     .ToArray();

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
  • ‹
  • 1
  • 2

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

27 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 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 avatar image

Related Questions

modifing enemies target inside a radius 1 Answer

Problem with GetComponentinChildren and Instantiate 0 Answers

How getting transform of another GameObject C#? 1 Answer

How to use "GetComponent" and "transform" code in dll file? 0 Answers

Endless 3D plane repetition animated by script is not moving 0 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