Day 112 — Designing Enemies using Abstract Classes

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
protected int health;
protected int speed;
protected int gems;
public virtual void Attack()
{
Debug.Log("Enemy Attack");
}
}
public class MossGiant : Enemy
{
void Start()
{
Attack();
}
}
public class Spider : Enemy
{
void Start()
{
Attack();
}
public override void Attack()
{
Debug.Log("Spider Attack");
}
}

Abstract Class

Now this works out fine for us currently as we don’t need to worry about every Attack() method being unique and our different enemies can share them but what if we needed to implement a method that had to be unique for every enemy type and must be used?

public abstract class Enemy : MonoBehaviour
{
protected int health;
protected int speed;
protected int gems;
public virtual void Attack()
{
Debug.Log("Enemy Attack");
}
public abstract void Update();
}
public override void Update()
{
Debug.Log("Updating the Moss Giant...");
}

--

--

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
Connor Fullarton

Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.