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
2
Question by useless.unity.user · May 23, 2012 at 11:18 AM · componentgeneric

Get Component by T

How could I get all components in a generic method by T?

currently I get an error saying that T must be convertible to UnityEngine.Component. Thanks!

 private List<T> GetComponent<T>(GameObjects[] objects)
 {
     List<T> compList = new List<T>();
     foreach(GameObject o in objects)
         compList.Add(o.GetComponent<T>());

     return compList;
 }
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

4 Replies

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

Answer by Kryptos · May 23, 2012 at 01:51 PM

If you also want to be able to get components by the interface(s) they implement, don't use the generic restriction but this code instead:

 private List<T> GetComponent<T>(GameObjects[] objects)
 {
     List<T> compList = new List<T>();
     foreach(GameObject o in objects)
        compList.AddRange(o.GetComponents(typeof(T)));
 
     return compList;
 }
Comment
Add comment · Show 3 · 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 useless.unity.user · May 24, 2012 at 02:17 AM 0
Share

the others are right, but I think this might be more flexible for my needs. Thanks!

avatar image Bunny83 · May 24, 2012 at 02:37 AM 0
Share

Yes, interfaces are quite handy ;)

Btw. I think the function should be called GetComponent*s* ;)

avatar image whydoidoit · May 24, 2012 at 04:21 PM 0
Share

Very cool - I've been iterating them and using Linq - very helpful to know that...

avatar image
8

Answer by fholm · May 23, 2012 at 12:29 PM

You're missing the "where T : UnityEngine.Component" constraint on the type parameter T.

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 Bunny83 · May 23, 2012 at 12:46 PM 0
Share

Yep this is the correct solution

avatar image flamy · May 23, 2012 at 01:04 PM 0
Share

indeed it is :)

avatar image
1

Answer by whydoidoit · May 23, 2012 at 11:25 AM

Any script attached to a GameObject is always derived from Component. You need use a T which is a derivation of it.

What are you trying to retrieve?

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

Answer by lyzard · May 23, 2012 at 12:28 PM

If I understand right what you want, you need to tell explicitely that T is a subclass of MonoBehaviour, that is:

 private List<T> GetComponent<T>(GameObject[] objects) where T : MonoBehaviour

I changed GameObjects by GameObject too.

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 Bunny83 · May 23, 2012 at 12:47 PM 0
Share

This will only allow $$anonymous$$onoBehaviours to be used as generic parameter. What if you want to use "Renderer" or "Transform"

this constraint is too restrictive

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

9 People are following this question.

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

Related Questions

Convert UnityEngine.Component to T 1 Answer

Is there a type which can refer to both GameObject and MonoBehaviour? 1 Answer

2D Animation does not start 1 Answer

Error CS0425: Works in DLL but not in Unity 0 Answers

Syntax for method wich returns generic type (own class)? 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