Using Abstract Classes for enemy creation.

Abstract classes allow one script to handle all basic code for things that use the same code. This means that for anything that uses duplicate code such as enemies, you can create a single script that houses all of it. This way you only need to write the code once, and well make unique alterations within a specific enemy if needed.

The first thing that I needed to do was to create a script called Enemy and figure out what each of my enemies would need. First would be a health, speed, gems and an Attack Method as all my enemies would attack. Let us start from here.

My first monster will be a MossGiant, and the biggest thing here is that the MossGiant inherits from NOT MonoBehavior, but the Enemy script. With that my actual MossGiant has access to all the methods and variables that are a part of the Enemy script. It can even use MonoBehavior because it inherits it from Enemy.

What is interesting is that by making it abstract, it actually forces the inheriting script to use the methods in the parent script. You can even do this separately for specific methods.

Lastly, you can do override to override any methods in the parent, this is where you are able to put unique coding for a special enemy. When doing override as well, it will automatically add a base.(method) which tells to do the already existing code from the parent. You can delete this and also add to it as I show here.

With the use of Abstract Classes, I was able to use it for both these enemies. The skeleton also took way less time as well to create as the Abstract Class saves tons of time. Now, for the 3rd enemy I have planned it is going to need some more unique code but should not be a problem.

Abstract classes was mind blowing for me. It was so much fun learning about it and I can’t wait to use it in more projects!

See you next post!

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Hello World! My First App

MY GOALS FOR THE ZURI INTERNSHIP

Project Management Debugging Logs

Comparing App Gateway v.s. Ngnix Ingress Controller for Latency in AKS

GCP ACE — Understand GCP Networking

Traefik Reverse Proxy

(python3) LeetCode (1) Two Sum

Deploy a macOS instance on Amazon EC2

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
Edward Kim

Edward Kim

More from Medium

Toss a Coin: Creating a Simple Distraction System (Game Dev Day 62)

Adding shields to some enemies

A Simple Shop System for a Unity 2D Game — Part 1: Graphics and UI

Collectables for the Player