Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
3
Question by Smireles · Jun 05, 2016 at 07:59 AM · stringcomponentsaddcomponent

How can I AddComponent() now?

I have a List of Dictionaries with the info (mostly strings) for a set of GameObjects to be spawn, this list will be generated on the go later on... for now I'm hardcoding the whole list.

At the moment of giving each GameObject an specific Script Component behavior I found myself a little lost. I noticed that in previous version of Unity people could use

 gameObject.AddComponent("ScriptNameHere");

Now this is not possible and I, as a noob, am clueless in how to implement my desired Monobehavior script in the set of GameObjects I want to Instantiate.

Maybe there is a better way to do this and I'm thinking wrong the whole solution. Please help.

Comment
Add comment · Show 1
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 Smireles · Jun 05, 2016 at 08:01 AM 0
Share

Perhaps I can give the reference of the Script in the Dictionary, and later use that Type of Component to be added to the GameObject... Hmmm /shrugs

3 Replies

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

Answer by Bunny83 · Jun 05, 2016 at 08:58 AM

The string versions of GetComponent and AddComponent has been deprecated. You have to get the Type manually:

 var type = Type.GetType( "ScriptNameHere" );
 gameObject.AddComponent( type );

Note: This only works for scripts defined directly inside Unity. Type.GetType can't return a type that isn't defined in the same assembly / DLL.

For this you could use either Unity's helper class "Types" which also has a GetType method but in addition requires an assembly name, or you could iterate through all assemblies using System.AppDomain.CurrentDomain.GetAssemblies() and check each assembly if your type is defined there.

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 Smireles · Jun 05, 2016 at 05:45 PM 0
Share

Since I'm working in c#, I would have to go with the Types solution and iterate through Assembly[] (right now it is only 39, but this will grow and I don't feel ok doing this).

Thanks for your suggestion and to go along with my initial request in using a String variable as the component name I want to include.

This would be the correct answer to my question.

$$anonymous$$y solution was to take another approach. I created a static class with a function that using a switch (with my string component name) will assign to my gameObject (also passed as a parameter) the desired Component using: targetGameObject.AddComponent(); Like the previous guys suggested...

Sigh.. It might be a pain to have to keep updated the switch cases but it is less painful than the iteration assemblies (at least for my computer).

avatar image
2

Answer by tanoshimi · Jun 05, 2016 at 08:56 AM

This was removed in Unity 5.0. Please read the following blog post for an explanation: http://blogs.unity3d.com/2015/01/21/addcomponentstring-api-removal-in-unity-5-0/ or, tl;dr, use the generic version gameObject.AddComponent<ScriptNameHere>();

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 Smireles · Jun 05, 2016 at 05:53 PM 0
Share

Thanks for your answer! I worked-around using the generic version of AddComponent. /bow

avatar image Pyunityer8002 · Aug 01, 2020 at 02:51 AM 0
Share

With or without quotation marks?

avatar image
1

Answer by Griffo · Jun 05, 2016 at 08:23 AM

If you already have your script saved .. In C#

 AddComponent<ScriptNameHere>();

Should add the script ..

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 Smireles · Jun 05, 2016 at 05:50 PM 0
Share

At the end I used this way to add the desired component. Created a switch inside a static function using my stringname and in each case to assign the component like you mentioned.

This is a different approach for my initial problem. That's why I marked as accepted the answer that played along with the issue I first described. Thanks for your input and to make me think in other workarounds.

(if I'm using erroneously the answering/rating system of this page, let me know.)

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

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

how to access to a component previously removed and readded 0 Answers

\n Not picked up when reading string from file's name 1 Answer

checking a bool via string 0 Answers

I need help with calling a string 1 Answer

Convert keycode to string 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