- Home /
Question by
NewPersona · Jan 06, 2018 at 10:21 PM ·
typeaddcomponent
Trying to get type by passing a string
I'm trying to add a component to a GameObject by passing a string to it.
GameObject newGO = new GameObject();
string supportCardType = "mySupportCardType";
Type newType = Type.GetType(supportCardType);
newGO.AddComponent<newType>();
This doesn't compile because: 'newType' is a variable but is used like a type.
Comment
Best Answer
Answer by NewPersona · Jan 06, 2018 at 10:23 PM
GameObject newGO = new GameObject();
string supportCardType = "mySupportCardType";
Type newType = Type.GetType(supportCardType);
newGO.AddComponent(newType);
It turns out that I just needed to use: AddComponent(); As opposed to : AddComponent();
Your answer
Follow this Question
Related Questions
C# - passing a Type var to GameObject.AddComponent 1 Answer
GetType is Monoscript, how to implement correctly? store type in variable? 1 Answer
AddComponent with runtime type checking 0 Answers
Assign a variable of type "type" on the inspector or best work around 2 Answers
Creating a Script at runtime, and adding it to an object... 1 Answer