Unity Guide

Using abstract classes to design enemies | Unity

A quick guide about how to use an abstract class to implement enemies in Unity

Fernando Alcantara Santana
Nerd For Tech
Published in
5 min readOct 7, 2021

--

Objective: Use abstract classes to implement enemy mechanics in a 2D game with Unity.

In the last post I covered how to implement a jump mechanic for our player with Unity. Now, it’s time to start implementing the enemy mechanics in our 2D game by using an abstract class or virtual methods.

Creating the enemies

Sprites

To start, let’s create a couple of new gameobjects with a sprite to represent the new enemies of our game. For our purpose, we’ll be using the sprite of a spider and a moss giant:

Classes

Then, in order to implement our enemies by using an abstract class, let’s create 3 new C# scripts:

  • Enemy

This class will be abstract and it will represent the base of every enemy subclass that we’ll use in our game.

  • Moss Giant

This subclass will inherit from the Enemy class and will handle the principal enemy mechanics and the moss giant mechanics.

  • Spider

This subclass will inherit from the Enemy class and will handle the principal enemy mechanics and the spider mechanics.

Now, in order to indicate that the Enemy class will be the base of the enemy subclasses, let’s:

  • Open the Enemy script and add the abstract keyword:

Note: It’s important to indicate that the Enemy class is abstract so that we can create abstract methods in it.

  • Open the MossGiant and Spider classes and change the inheritance from MonoBehaviour to Enemy:

Variables

And now, as we know that each enemy subclass inherits from the Enemy class, we can create new variables that we know that every enemy will need to have (like health, speed or gems that it will drop when it gets defeated):

Don’t forget to use [SerializeField] to be able to modify the values through the inspector.

Note: We’ll need to set the variables as protected (or public) to be able to read and use the variables within the enemy subclasses.

This way we’ll be able to attach the respective scripts to our enemies and modify its values through the inspector:

Virtual methods

And now, let’s implement a method that each enemy could implement from the base class: Attack. By using a virtual method we’re able to use or overwrite it within the enemy subclasses. Let’s declare the virtual Attack method and print a message when it’s called:

Then, let’s declare the override Attack method within the enemy subclasses. With the override keyword we’re able to overwrite the method from the Enemy base class or also call it within the new implementation by using the base.Attack line:

Now, in order to see how the virtual methods work, let’s use the Start method within the Enemy subclasses to call the Attack method:

If we run the game with Unity, we’ll be able to see that the Attack method from the Enemy base class gets called first (thanks to base.Attack) and then the attack method from the subclasses continue with the execution:

Abstract methods

Finally, let’s implement an abstract method for our base class. An abstract method implies that it’s mandatory to implement the same method within its subclasses. So, for example, let’s implement the Update method as an abstract method:

Note: When we declare an abstract method within our abstract class, we can’t define any functionality.

Once Update is declared as an abstract method we’ll be able to see that the compiler displays an error within the subclasses of Enemy:

This error gets displayed because we should implement the Update method mandatorily in the subclasses. So, in order to fix it, let’s implement the override Update method in the MossGiant and Spider subclasses:

This way we can make sure that each enemy that we create implements the Update method mandatorily. By using abstract classes or virtual methods we’re able to deliver a functional and organized set of classes that use similar mechanics like the enemies of our game.

And that’s it, we used abstract classes to implement enemy mechanics with Unity! :D. I’ll see you in the next post, where I’ll keep working in the development of our 2D game with Unity.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

--

--

Fernando Alcantara Santana
Nerd For Tech

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).