- Home /
Question by
VesuvianPrime · May 31, 2013 at 05:10 PM ·
c#variabletypeaddcomponent
C# - passing a Type var to GameObject.AddComponent
Hi all!
I'm in a situation where I'm picking a class to assign to my GameObject from a list at random. I'm 99% of the way done, I've just hit one little snag:
Type enemyClass = randomEnemyClass();
AbstractEnemy enemy = enemyInstance.AddComponent<enemyClass>();
Yields the error:
Assets/Scripts/Quadrant.cs(156,59): error CS0246: The type or namespace name `enemyClass' could not be found. Are you missing a using directive or an assembly reference?
This likely comes down to my lack of understanding of the C# compiler. Can anyone offer the correct way to add my component?
Thanks, Ves
Comment
Your answer
Follow this Question
Related Questions
Vector3 is a 'type' but it is used like an 'variable' (C#) 1 Answer
C# string as type for AddComponent 2 Answers
How to declare the type after a string? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers