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.
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
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.
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).
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>();
Thanks for your answer! I worked-around using the generic version of AddComponent. /bow
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 ..
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

Follow this Question
Related Questions
how to access to a component previously removed and readded 0 Answers
Pick a random string from a string array C# 1 Answer
use the tag of a gameobject to assign a string variable 1 Answer
How to round to 2 decimals with format? (123456 > 123K > 123.45K) (C#) 0 Answers
Help int.Parse "Input String was not in the correct format" PROBLEM 0 Answers