Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Bokaii · Oct 26, 2014 at 02:00 AM · multiplayerfpspathfindingonline

Find children of object and store in an array

How do I find the children of an object, and store them in an array?

I have a wall, with 4 points. I wan't to be able to access these points, but in order to do that I need to have them in an array.

I use C#

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

3 Replies

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

Answer by Kiwasi · Oct 26, 2014 at 07:20 AM

For a one line method to get the array

 Transform[] children = GetComponentsInChildren<Transform>();

Transform is also enumerable. So you can avoid the array altogether with:

 foreach (Transform child in transform){
     // Do something to child
 }
Comment
Add comment · Show 7 · 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 Bokaii · Oct 26, 2014 at 07:57 AM 0
Share

Thank you for your answer, I works.... $$anonymous$$inda. When I use this 1 line method, I don't get the children only. I get the parent as well! Why???

$$anonymous$$y code:

 public GameObject test;
 public Transform[] points;
 
 void Update(){
     points = test.GetComponentsInChildren<Transform>();
 }
avatar image fafase · Oct 26, 2014 at 08:05 AM 0
Share

Here is the doc description:

 Returns all components of Type type in the GameObject or any of its children.

If you want to remove the parent:

 var children = Array.FindAll(GetComponentsInChildren<Transform>(), child => child != this.transform);

but I think the parent is always the first one so you could start iteration from second index (1). But I am not 100% sure about that.

avatar image Kiwasi · Oct 26, 2014 at 08:11 AM 0
Share

Huh, learned something new today. Thanks for the heads up.

avatar image Bokaii · Oct 26, 2014 at 08:29 AM 0
Share

What is the:

 child => child != this.transform);

??

avatar image Kiwasi · Oct 26, 2014 at 08:38 AM 0
Share

lambda expression

Its a weird, but very powerful C# language feature.

Show more comments
avatar image
6

Answer by AlwaysSunny · Oct 26, 2014 at 01:46 AM

Based on your questions I've seen here, you'd really benefit from spending an afternoon in the Learn Unity area of the website, where lots of tutorials address a ton of beginner's questions while teaching good practices.

You probably want this:

 Transform[] children = new Transform[transform.childCount];
 for (int i=0; i<transform.childCount; i++) {
     children[i]=transform.GetChild(i);
 }


Comment
Add comment · Show 2 · 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 Bokaii · Oct 26, 2014 at 07:12 AM 0
Share

So with this, I could do something like:

send a raycast and check if I hit a certain object. If I hit that object, then get and store the objects children in an array?

avatar image W4rf4c3 · Nov 24, 2017 at 01:31 PM 0
Share

Simple and working solution

avatar image
0

Answer by mzr_developer · Jul 09, 2019 at 09:05 AM

 List<GameObject> children = new List<GameObject>();

 void Start()
 {
   foreach (Transform t in transform)
    {
      children.Add(t.gameObject);
    }
 }


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

6 People are following this question.

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

Related Questions

Find closest object with tag 1 Answer

Remove first element of array 1 Answer

Find closest transform 1 Answer

Closest array element 1 Answer

Add one on local z axis 3 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