• 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 AlecGamble · Jul 06, 2016 at 12:06 PM · variablegetcomponenttypeparameters

AddComponent with parameter variable

Hi guys.

I'm creating an RPG and I want to be able to add and remove Spells (Monobehaviours) from my character dynamically. So two functions on my Player script to add and remove Spells from the player's GameObject. The problem is I want to pass the spell, name of the spell, whatever as a variable to these methods. So Explicitly I can say:

     void AddSpell()
     {
         gameObject.AddComponent<Meteor>();
     }
     void RemoveSkill()
     {
         Destroy(GetComponent<Meteor>());
     }

that's fine. However I'm unsure how to pass my Meteor Monobehaviour (or any other Spell Monobehaviour) as a variable. I've done some reading and I figure I can't use AddComponent<T>() with a variable because the Type has to be defined at compile time but I can use AddComponent() right? However I obviously can't create a New instance of any Monobehaviours. I can't use gameObject.AddComponent(Resources.Load("").GetType) because then it's saying that GetType is a method group instead and I don't know how to store Meteor as a type to send as a parameter.

Any ideas?

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

2 Replies

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

Answer by Fire_Cube · Jul 06, 2016 at 12:26 PM

You can use the .GetType() or typeof(type) to get the type, and then pass it in parameter like this :

 AddComponent(type);

It's as simple as that :)

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 AlecGamble · Jul 06, 2016 at 12:28 PM 0
Share

ah, I was using GetType() and that was throwing up errors for me but typeof() is working fine.

Thank you.

avatar image
0

Answer by Bunny83 · Jul 06, 2016 at 12:49 PM

Well, it depends on how you want to use your methods. There are many different ways.

First you could make your methods also generic methods.

 public void AddSpell<T>() where T : MonoBehaviour
 {
     T inst = gameObject.AddComponent<T>();
 }

If you have some sort of "Spell" base-class you might want to restrict the generic parameter to that base class instead of MonoBehaviour. Of course a generic class can only do "generic" things to the object based on the generic constraints.

You would use that method just like the AddComponent method:

 player.AddSpell<Meteor>();

Second you could pass a System.Type reference:

 public void AddSpell(System.Type aType)
 {
     Component inst = gameObject.AddComponent(aType);
 }

This allows you to add any component class to your object. However, just like the generic version inside your method you can only access your component in a generic / abstract manner. If you always use a certain baseclass you can cast the "inst" reference to that base class.

This version could be used like this:

 player.AddSpell(typeof(Meteor));

This version allows you to use a variable for the type

 System.Type someVar = typeof(YourComponentClass);
 player.AddSpell(someVar);

You did not give a concrete usecase so we can't give a concrete example.

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

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

46 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

Related Questions

Passing a Script Name to a Function 2 Answers

How would I make the GetComponent field a variable? 1 Answer

How could you access a script of varying name? 5 Answers

Is it possible to use a variable as a Type? 1 Answer

C# variable type problem, always get NullRefExc 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