- Home /
How to store class type in variable
I need a method to store class type in variable, so it can be like
private ClassType classType;
//....................
classType = Regeneration; //Regeneration - some class, extending base Skill class
gameObject.AddComponent<classType>();
//....................
Skill skillComp = (Skill)GetComponent<classType>();
Ok, regarding answers i got. "Skill" class i mention extends $$anonymous$$onoBehaviour. Currently i use System.Type as variable type to store class type, and AddComponent adds new component just fine, but GetComponent with same class type variable doesn't detects anything.
Answer by Itaros · Oct 26, 2014 at 08:57 PM
.NET Reference is viable source of information regarding advanced C# aspects: http://msdn.microsoft.com/en-us/library/system.object.gettype%28v=vs.110%29.aspx
Yeah, yeah. I have whole system based on System.Type. But it doesn't work properly. GetComponent(skill), where skill is System.Type variable, which stores type i need, gets nothing.
I add component with AddComponent(typeVar), and then trying to get this component with GetComponent(typeVar), and it gets nothing.
$$anonymous$$ight be a bug then. Try GetComponent(typeVar.Name); to see if it works at all.
Answer by Landern · Oct 26, 2014 at 09:50 PM
For a Component to be attached to a GameObject it must derive/inherit/extend from MonoBehaviour. You can put the custom type on a class that does infact inherit from MonoBehaviour and use it like any other object type(field/property/etc).
Actually, components i use are extending $$anonymous$$onoBehaviour. See my comment to prev answer for more info about situation.
Actually, components i use are extending $$anonymous$$onoBehaviour. See my comment to prev answer for more info about situation.
Not helpful to the people trying to help you, this was posted 3 $$anonymous$$utes ago, my answer 16 $$anonymous$$utes ago. Perhaps you should include relevant information in your original post. No one can assume your skill or knowledge level
$$anonymous$$b you should check another answers and comments before posting your own. Comment where i clarify what problem i have was posted 10 $$anonymous$$s before your answer. And that's not the case of question -_-Not helpful to the people trying to help you, this was posted 3 $$anonymous$$utes ago, my answer 16 $$anonymous$$utes ago.
It is getting intense there. In fact original question diverged into GetComponent troubleshooting. Landern is right.
Once again, we can not assume anything, no where do you specifically detail that your inheritance includes $$anonymous$$onoBehaviour, since that wasn't detailed, it was likely your first issue. I hope you find what you need.
Your answer
![](https://koobas.hobune.stream/wayback/20220613170022im_/https://answers.unity.com/themes/thub/images/avi.jpg)