Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Fritsl · Mar 10, 2017 at 09:56 PM · types

Juggling with Type as a variable

I'll have to test for a lot of 'Types', and to avoid repeating a very long almost identical code, I'm trying to make it a function.

But I'm not sure how to pass 'Type' as a parameter?

This works / hardcoded:

     foreach (MeshFilter one in checkthis.GetComponentsInChildren<MeshFilter>())

now, I'm trying to set 'Meshfilter' up to be a variable instead, like so:

 foreach (thisType one in checkthis.GetComponentsInChildren<thisType>())

  • But I cannot write a proper variable for 'thisType', so it can be for example MeshFilter SkinnedMeskRenderer, sharedMaterial.. Is this even possible, how would you?

Thanks a bunch @Bunny83 ?

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
0

Answer by roojerry · Mar 10, 2017 at 10:09 PM

Could you use the other overload of GetComponentsInChildren that takes a type as an argument. Changing your foreach to:

 foreach(var one in checkthis.GetComponentsInChildren(thisType))
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 Fritsl · Mar 10, 2017 at 10:19 PM 0
Share

Thanks - one might, but I do not appear to be able to write it right.

 --
 doButtons(checkthis, $$anonymous$$eshFilter);
 --
 
 void doButtons(GameObject checkthis, Type thisType)
 {  
 foreach (var one in checkthis.GetComponentsInChildren(thisType))
 {
 //--
 }
 }

Error: UnityEngine.$$anonymous$$eshFilter' is a type' but a variable' was expected

avatar image roojerry Fritsl · Mar 10, 2017 at 10:21 PM 0
Share
 doButtons(checkthis, typeof($$anonymous$$eshFilter));
avatar image Fritsl roojerry · Mar 11, 2017 at 07:41 AM 0
Share

@roojerry thanks - but inserting '$$anonymous$$eshFilter' really does not make any sense: I am trying to figure out how to write '$$anonymous$$eshFilter' as a type-variable, so it can be for example $$anonymous$$eshFilter OR Skinned$$anonymous$$eskRenderer OR shared$$anonymous$$aterial..

A$$anonymous$$A a variable that can indicate the Type… am I not making any sense?

Show more comments

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

Unknown identifier: 'types'. js(38,61) 1 Answer

cannot convert UnityEngine.Gameobject to GameObject - What's the difference? 1 Answer

InvalidCastException with .net datatypes but not with UnityEngine.Object 1 Answer

How to make a custom type's contents modifiable in an array by inspector? 1 Answer

C# return question 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