I Am Your Father! — Abstract Classes In Unity
In this article, I will go over what are, and the advantages of, using abstract classes in Unity.
What are abstract classes?
Abstract classes allow us to create a base class that we can then inherit from. When we use class inheritance we enable the new class to use all the functionality from the base class without the need for retyping everything. It's a great way to save time, group classes and reduce the chance for errors.
Why use abstract classes?
Consider the following scenario; you’re creating your first enemy for your game (let’s say a spider). You write up the script for the spider containing the following:
You program in all the functionality and everything works. Now you create your next enemy, a skeleton. The skeleton needs to have a health value, speed, reward when killed and needs an attack method. It would look like the following:
We have the same base information in both scripts. This isn’t going to cause many issues with only 2 enemies, but what happens as we add more enemies in, then if we want to add an attack strength to all of our enemies?
In this scenario, we need to go into each enemy script and add this value. Which is time-consuming and prone to error. Fortunately using abstract classes we can implement our functionality easily by providing variables and methods in our base class and inheriting them in our child classes.
How to create and use abstract classes?
The first step to creating an abstract class is to create a script. I have created one called “Enemy” using the same variables and methods as the above scripts and we can add the keyword “abstract” to the declaration of the class.
Next to allow the child scripts to be able to access and modify the variable values we can change all of the access declarations from private to protected.
We want each of our enemies to have an attack method, but may have different attack styles (ranged, melee, etc.) we can create a virtual method using the declaration ‘virtual’
This method can be called on the child class using the override keyword.
I’ll create a child class (MossGiant) to demonstrate this. On the moss giant class instead of inheriting from MonoBehaviour, we will inherit from the Enemy class.
Now we can add our override method.
What you may notice is that we have base.Attack automatically being added. This means that we will call the implementation of Attack from the base script. This can happen before or after the child scripts implementation or can be removed completely.
What you may notice though is a virtual void is not a mandatory method.
If you want to force the child classes to implement a particular function we can use an abstract function using the “abstract” keyword. The function can not have any implementation so it is a great way of forcing a class to use a function without forcing a particular implementation of that function.
If we think about our example with enemies having either a ranged or melee attack, an abstract method would suit this purpose.
What you may notice now on the child classes is they contain an error:
We can right-click the error and select “Implement Missing Members” which will add any missing member functions.
You have now successfully created and used an abstract class.
That’s all for now.