Day 119: Designing Enemies using Abstract Classes

Objective: Create enemies using Abstract classes.

We will be creating some enemies this round. Our enemies will have variables for speed, health, and gems. We are going to have our enemies move using waypoints. We will use a abstract class to keep the code clean and not have redundant code.

We will start by creating a enemy script. In our enemy script, we will have to figure out what all the enemies will have the same code. Starting off right now we know our enemies will need health, movement speed, and the amount of gems they’ll drop when they die.

With the script open, we will add abstract before class to turn this script into a abstract class. Next we will create all the variables we need. Instead of doing the normal public or private type variables we will set them to protected. We have them protected to allow the child scripts to read the variable but not change.

If we want will want the child object to handle their own update or attack method, we will force each child to implement the method. We do this by adding abstract in front of void.

The next thing to do is when creating another enemy script, we will name the script by the name it is. Once created we will remove monobehaviour and replace it with Enemy.

One thing to know for sure is that abstract classes can not be added as a component to a game object but the children scripts can.

This was a quick guide to design an abstract class for other’s to inherit from. Thank you for reading and have a wonderful day!

--

--

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
Tyler Smallwood

Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer