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
1
Question by Ochreous · Jun 26, 2013 at 10:00 PM · c#arraygetcomponentsinchildren

C# Specifying which Children to add to the Array

Hi everyone, is there a way to get GetComponentsInChildren(); to specify which children to add to someTransformArray? I want to add some of the gameobject's children to the array but not all of them.

 Public Transform[] someTransformArray;
 
 void Awake(){
 someTransformArray = GetComponentsInChildren<Transform>();
 }
Comment
Add comment · Show 3
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 iwaldrop · Jun 26, 2013 at 10:08 PM 0
Share

Get components in children will return only those game objects who have the specified component attached. At that point you have to scrub the list based on some other criteria.

avatar image Ochreous · Jun 26, 2013 at 10:24 PM 0
Share

What other options do I have?

avatar image iwaldrop · Jun 26, 2013 at 10:30 PM 0
Share

I don't know what you mean, what other options. You get all the components, and filter the ones you don't want. That's what you're saying you want to do. Verbalizing it helps sometimes to realize how trivial a problem is. If you shared what you want to filter out then you might get more help.

1 Reply

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

Answer by BlueRaja_2014 · Jun 26, 2013 at 10:27 PM

If I understand correctly, you want to filter the return values from GetComponentsInChildren() to only select the values of type Transform and set it equal to an array?

Try this:

 someTransformArray = GetComponentsInChildren().OfType<Transform>().ToArray();

It reads almost like English; gotta love LINQ :)

Note: You may need to include using System.Linq to use `OfType()`.

Comment
Add comment · Show 5 · 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 BlueRaja_2014 · Jun 26, 2013 at 10:31 PM 0
Share

@iwaldrop Does he want a list of the Transform-property values from each component, or a list of components which are of type Transform? Those are two different questions.

I answered the latter; the former can be done with
GetComponentsInChildren().Select(o => o.Transform).ToArray();

avatar image Ochreous · Jun 26, 2013 at 10:31 PM 0
Share

No I simply want to specify which children the array picks. There are some children in my gameobject that I don't want to add to the array.

avatar image iwaldrop · Jun 26, 2013 at 10:32 PM 0
Share

Add a .Where((x)=> x == somethingYouWant) to your LINQ query.

avatar image BlueRaja_2014 · Jun 26, 2013 at 10:34 PM 0
Share

@DangerousBeans
That is also a one-liner in LINQ. For example, to get all the components with positive X-value:
GetComponentsInChildren().Where(o => o.X > 0).ToArray();

avatar image clunk47 · Jun 26, 2013 at 11:01 PM 0
Share

Good answer +1 :)

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

18 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

Related Questions

C# SetActive GameObject Array 2 Answers

C# Children and SubChildren Null Reference Exception 1 Answer

How to check if an object is colliding with another from another script ? 1 Answer

How to check if string array contains duplicates C# 1 Answer

Finding Distance between Angles and Points 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