• 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 NinjaSu · Nov 08, 2012 at 08:59 AM · getcomponentgeneric

Get Components In Children

I'm writting a helper class which I will give a gameObject and it will return me an array of the children scripts of the gameObject. I can do this if I know the name of the child script. For example:

     public ChildScript[] getChildrenList(GameObject parent)
     {
       ChildScript[] array = parent.GetComponentsInChildren<ChildScript>()
     
       return array;
     }

But I want to reuse this code for many different Child Scripts. How can I make it be "generic"? If that is even possible.

     public T[] getChildrenList(GameObject parent)
     {
            T[] array =  parent.GetComponentsInChildren<T>();
         
         return array;
     }

This code produces this error: The type 'T' cannot be used as type parameter 'T' in the generic type or method 'UnityEngine.GameObject.GetComponentsInChildren()'. There is no boxing conversion or type parameter conversion from 'T' to 'UnityEngine.Component'.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Kryptos · Nov 08, 2012 at 09:34 AM

The question is why do you need to write a method that already exists?

Anyway, you need to constrain the generic type T to be derived from component :

 public T[] getChildrenList(GameObject parent) where T : Component


Note: If you want to be able to retrieve components based on their interface (if you use interfaces in your scripts) then you cannot use the generic version of GetComponentsInChildren but must rather use the typed version:

 public T[] getChildrenList(GameObject parent)
 {
     return parent.GetComponentsInChildren(typeof(T));
 }

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 NinjaSu · Nov 08, 2012 at 09:44 AM 0
Share

I want to say: Find me all the Children inside this gameObject and not tell it what the children are.

avatar image Bunny83 · Nov 08, 2012 at 09:54 AM 0
Share

This answer is missing the generic parameter ;)

public T[] getChildrenList(GameObject parent) where T : Component

Anyway, like $$anonymous$$ryptos said this function does EXECTLY the same as GetComponentsInChildren, so i'm also not sure what's the purpose of this function. Can you be a bit more specific what you want to do?

avatar image NinjaSu · Nov 08, 2012 at 10:00 AM 0
Share

You are both right, it is the same function. I want a function that I tell it, Find me all the Children-Scripts inside this gameObject and I don't have to say the children names.

avatar image whydoidoit · Nov 08, 2012 at 10:26 AM 0
Share

But that's just GetComponentsInChildren (except the it also returns those scripts on the parent too)...

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

12 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

Related Questions

get component type at runtime using generic GetComponent in C# 1 Answer

Generic Component Declaration 1 Answer

A generic "Find GameObject From Path" method. 2 Answers

Weird issue with boolean 1 Answer

Is it possible to assign the script for GetComponent in the editor? 1 Answer


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