- Home /
Add a component of type script dynamically at runtime.
I have enemy units in my game and each one has a different weapon script inheriting from monobehvaiour and an Iweapon interface.
What I'm trying to do is have a script which is defined in the scriptableobject for the enemy attached an enemy game object at runtime.
Enemies are spawned and I need a script to add the script specified in the scriptableobject to the gameobject. Right now I have ai.gameObject.AddComponent(enemyDataCont[index].Weapon.GetType()) as IWeapon;
Where ai is the EnemyAI script whose gameobejct I want to add the component too, and enemyDataCont being a list of the enemydata scriptableobjects.
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
enable and disable script on Unity gameobject 0 Answers
Cannot destroy Component while GameObject is being activated or deactivated 2 Answers
which is best scripting method among these 3 ? please view the image 1 Answer
I can't add a very simple Bolt flow to a gameobject 1 Answer