- Home /
Will using AddComponent frequently lower performance?
I'm creating a Scrolling Shooter/Shmup RPG that will be using a lot of unique enemies. I currently have an Event Director scripted that controls the spawning of enemies and objects. To speed things up in the long run, I was thinking about scripting a system that will use AddComponent to create unique enemies at run-time. I'd be adding various Movement and Combat scripts to a single prefab, which all enemies would derive from. In the long run, this would make creating new enemies an extremely fast process.
Now, before I decide to code the system... I'm wondering how much of an impact on performance this might make, as compared to making a ton of predefined enemies.
Example:
Instantiate enemy prefab
Modify common enemy values
Add move script (Various curves, linear paths, and staged events)
Modify values
Add combat script (Various bullet patterns)
Modify values
I'm not really answering your question, so I post only this as a comment. You can also put all your deactivated components in your prefab, and then on runtime activate only what you want. This should be better (performances speaking).
Answer by whydoidoit · Jan 22, 2013 at 10:13 AM
Well to start with instantiating at run time is a bit expensive in terms of performance and yes doing your own AddComponent isn't exactly brilliant either. How about you add all of the components to a prefab, instantiate a pool of (say) 20 enemies and then configure them by enabling and disabling the components that have been added - then your runtime performance would be much better and you should get the same result.
Your answer
![](https://koobas.hobune.stream/wayback/20220613091350im_/https://answers.unity.com/themes/thub/images/avi.jpg)