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 /
  • Help Room /
avatar image
1
Question by oliver-jones · Dec 09, 2016 at 12:17 PM · nullreferenceexceptionreturncastgetcomponentsinchildren

Get all Child Transforms in Target

Hello,

I, for some reason cannot get this to work...

All I'm trying to do is to get all the Child Transforms that are present within a target and return them as an array. I have done and followed examples online, including the API, and I get errors.

This is how I have it right now. This gets the return method of the Transforms[]

 //get all children on target object
 Transform[] _children = (Transform[])_target.GetChildrenOfObject();

Return method:

 public Transform[] GetChildrenOfObject(){
 
     Transform[] _transforms;
     _transforms = (Transform[])GetComponentsInChildren( typeof(Transform), true );
     return _transforms;
 }

When I run this, I get the error : Cannot cast from source type to destination type. So I tried:

 public Transform[] GetChildrenOfObject(){
 
     Component[] _transforms;
     _transforms = GetComponentsInChildren( typeof(Transform), _includeInactive );
     return _transforms as Transform[];
 
 }

Which returns a Null...

What am I doing wrong here? (Might be worth mentioning that this code is for an Editor script - so it gets called in Editor Mode, and Play Mode).

Comment
Add comment · Show 6
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 Link0n3 · Dec 09, 2016 at 12:48 PM 0
Share

@oliver-jones have you tried to get the transform with the generic version of that function?

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

Notice that you will get also the parent object with this query.

avatar image oliver-jones Link0n3 · Dec 09, 2016 at 01:43 PM 1
Share

Hello, this appears to work fine. However, not only does it take into account the parent Transform. It also includes all sub children of the children. How do I make it so it doesn't include the parent, and sub-children?

Thanks.

avatar image Ardaaytac oliver-jones · Dec 09, 2016 at 02:10 PM 0
Share
   public List<Transform> transformList;
     foreach (Transform item in gameObject.transform)
     {
         transformList.Add(item);
     }  

You could just use a foreach and transform list.

Show more comments
Show more comments

2 Replies

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

Answer by Bunny83 · Dec 09, 2016 at 02:12 PM

Don't use GetComponentsInChildren because it does exactly what you have described. It includes the parent as well as all component in any children, no matter how deeply nested.

The Transform class implements the IEnumerable interface. Unfortunately only the type-less version. Though a foreach loop can handle that directly:

 foreach(Transform child in transform)
 {
     // ...
 }


The second way, if you want the childs in an array or list, is to use Linq.

 // of course at the top
 using System.Linq;

 // as typed enumerable
 IEnumerable<Transform> childs1 = transform.Cast<Transform>();
 // as array
 Transform[] childs2 = transform.Cast<Transform>().ToArray();
 // as list
 List<Transform> childs3 = transform.Cast<Transform>().ToList();

The third way, without using Linq would be to use childCount and GetChild:

 int count = transform.childCount;
 for(int i = 0; i < count; i++)
 {
     Transform child = transform.GetChild(i);
     // ...
 }

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 Futurristic · Apr 28, 2020 at 11:43 AM 1
Share

In your view which way is best and safe way...

avatar image
3

Answer by Ralp7 · Dec 09, 2016 at 02:27 PM

I don't know what is wrong with your code but you can use generic version of GetComponentsInChildren function which works fine.

 _transforms = GetComponentsInChildren<Transform>();


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 joebain · Sep 12, 2017 at 04:18 PM 3
Share

Danger! This will return the current object as well as any transforms lower down the hierarchy.

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

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

Related Questions

NullReferenceException when using gameObject.SetActive(false); 0 Answers

Help needed finding NullReferenceException 1 Answer

So I know this has been asked before but, I feel this only applies to my problem. 2 Answers

NullReferenceException on a GameObject holding the main scene camera as the parent 1 Answer

Space Shooter Score Text NullReference error 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