Enemies design with abstract classes

Let’s say we have these enemies:

As we can see, all of them share the same variables, so we can create a class from which they can inherit those values

It seems to work fine so far, but there is a little drawback: all the enemies will execute Attack() in the same way. Let’s say we’ll call that method in Start() in both enemies:

And obviously we want each enemy to behave differently. For this, we can use abstract classes and methods. Further information about these topic can be found on internet, here we will use a simple implementation.

Keep in mind about abstract method:

That way each enemy will implement the Attack() method as desired, and it will be executed independently

Now, we will call Attack() in Start() in Enemy script and we will implement the first one in each enemy. It will be executed in the whole scripts that inherit from Enemy, even if they don’t call Attack() in their own Start() method



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store