• 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 itinerary · Mar 12, 2014 at 08:20 PM · c#variablegetcomponenttype

Is it possible to use a variable as a Type?

For instance:

Type myType = new Type //initialize somehow? I can't initialize System.Type as it is an abstract

//make changes to myType

myGameObject.GetComponent < myType >();

If this is possible, could you also then please demonstrate how to pass this to a function?

Many thanks

Comment
Add comment · Show 2
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 perchik · Mar 12, 2014 at 08:20 PM 0
Share

What are you trying to accomplish?

avatar image itinerary · Mar 12, 2014 at 10:21 PM 0
Share

I'd like to create a function that will look for a myType Type of Component in a GameObject (where myType is a variable). This is just to make things more modular, I'm curious if this is possible.

Otherwise I know how to pass a specific type to GetComponent.

1 Reply

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

Answer by wildex999 · Mar 12, 2014 at 10:36 PM

You looking for something like this?

 public Component getCustomComponent(string comp)
 {
     Type myType = Type.GetType(comp); 
     return myGameObject.GetComponent(myType);
 }

 getCustomComponent("MyCustomComponent");

 
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 itinerary · Mar 12, 2014 at 11:02 PM 0
Share

Yes!

I think the crucial bit which I got errors on was the declaration you've used for myType, specifically using

 Type myType = Type.GetType(comp) 

ins$$anonymous$$d of using

  Type myType = new Type 

as System.Type is an abstract

$$anonymous$$any thanks!

avatar image wildex999 · Mar 12, 2014 at 11:04 PM 0
Share

Be aware tho that Type.GetType can return null if it can't find the type, checking for this is a good idea ;)

avatar image itinerary · Mar 12, 2014 at 11:05 PM 0
Share

cheers! will do

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 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

Related Questions

AddComponent with parameter variable 2 Answers

Passing a Script Name to a Function 2 Answers

script with individual function for each gameObject or public script variable to solve inventory system problem 1 Answer

C# - passing a Type var to GameObject.AddComponent 1 Answer

How to call a function on an instance 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges