I'm making simple space shooter game for Android in Unity. I want to have different types of enemies (different weapon, stats, movement schemes) and I'm not sure how to do it. I'm quite new to objective programming and I don't really know what classes should I create. I guess I could just make a prefab for every enemy since there won't be many types, but that is not what I'm looking for. I have some ideas:

First of all I'd write an BaseEnemy class and enemy type subclasses that inherit from it.

My first idea is to create those without MonoBehaviour. Then I'd have to make an EnemyScript with MonoBehaviour that I'd add to enemy game object. It'd have a specific subclass object inside, which will set every variable. BaseEnemy would just have undeclared variables and in subclass I'd set particular values. But I'm not sure how to deal with different movement schemes then. I wouldn't be able to put move() function in subclass since it wouldn't have MonoBehaviour, so I'd have to find another way. I might put in EnemyScript every possible scheme and then use correct one depending on some type value, but it doesn't seem like a proper way, does it?

The second idea is to just make a MonoBehaviour BaseEnemy class. Then while creating enemy game object I'd just add correct subclass script (aaand... I'm not sure how to do it :/ )

Could you tell me which one is a better way? Maybe neither of those?


1 Answer 1


There are many different approaches, with different pros and cons. One good approach here, and the one most suited to Unity's entity/component system, is to simply mix and match different MovementBehavior, AttackBehavior, etc.components on different objects to make different enemies.

However that still runs into this problem you mentioned in your question, so I'm curious for clarification on what you're having a problem with there:

aaand... I'm not sure how to do it :/

Is your problem that you simply don't know how to subclass MonoBehaviour? Because you totally can, just like any other class. Technically you already do, you just might not realize it; the public class MyClass : MonoBehaviour { at the top of most scripts is inheriting from MonoBehaviour.

Thus you can simply make a public class BaseEnemy : MonoBehaviour for your enemies to inherit from, and then make a script public class SpecificEnemy : BaseEnemyfor specific enemies. Now you can just drag the SpecificEnemy script onto gameobjects just like any other component.

ADDITION: This other thread alludes to the same issue and solutions, so you should read it.

  • \$\begingroup\$ I'm not sure how to add a particular script via other script. I mean, I'd like to have an object that spawns enemy waves. I'd also like to avoid to create more than one prefab. I obviously can use AddComponent() function, but I need to store all the possible components I want to use in game. So it would be nice to have some array or sth with for example MovementBehaviour scripts. \$\endgroup\$
    – Maras
    Jun 15, 2017 at 21:03
  • \$\begingroup\$ ah that's a job for the factory pattern google.com/… \$\endgroup\$
    – jhocking
    Jun 15, 2017 at 21:51
  • \$\begingroup\$ So like you'd just call EnemyFactory.MakeBomber() and it'll handle the details. In that class you'd list all the components needed. \$\endgroup\$
    – jhocking
    Jun 15, 2017 at 21:52

Not the answer you're looking for? Browse other questions tagged .