Understanding IDamageable Interfaces (part 1)

Creating the damage system using Interfaces

Since for the damage system we are NOT going to implement a damage method for each and every single enemy, and because it is not optimized to scan for every Trigger name object, we are going to use another method. This new method is very similar to Abstract classes but instead of being inherited, we are going to implement it. This damage system we are going to be using something similar to Abstract classes, this new method is called Interfaces.

Understanding IDamageable Interface:

Interfaces can be attached to any class in any amount, but classes can only inherit only ONE class only. That is the advantage that Interfaces have over Abstract classes.

The IDamageable interface basically simplifies and optimizes the workflow for damageable objects and or enemies. The Traditional way of creating a damage method would be to have our sword object scan whatever “Tag” it collides with. But this method becomes heavy once you introduce multiple of enemies and objects that need to be scanned. But for the Interface instead of scanning for a name tag or checking for colliders, we are going to check for a Interface script. For example the player sword can how swing and hit varies of objects within the scene, and if the object that collides with the sword has an IDamageable interface, then the Damage method is going to be invoked. It doesn’t require a compare Tag function or name scan.

Scripting IDamageable:

To begin we are going to create a new C# Script which we will be our IDamageable Interface script. Instead of Using Monobehavior or even the class system, we are going to use the “Interface” system.

Because this is an Interface instead of a regular Class, we are unable to use “Fields” which are our regular variables and constants such as bools or Integers or floats. Instead we have to use “properties” which are similar to variables but they have something called “Getters” and “Setters”. These Get and Set ability lets us control what values can be retrieved and placed. For example, I can Get and Set the Health value for each enemy spawn.

Getting and Setting the Health Value by using properties.
Adding a Damage Method to the Interface

Now that we have the basics of the Interface down, we can start applying them to our enemies. In the Moss Giant script we can start applying the Interface by adding a comma beside the Enemy and typing “IDamageable”.

You will noticed that immediately that there will be an error, this is because we are missing several components before this script can meet the conditions. Remember since Interfaces are contracts that must be fulfilled, classes that implement interfaces must abet by them.

In this example, the Moss Giant script is missing both the Health value and Damage Method.

As you can see, once the two conditions have been met, the error for the Interface goes away.

And for those you caught on we now have two Health variables, one from the parent Enemy Script and one from the Interface script. To solve this issues we are going to basically assign the property value as our Abstract class value.

Now the Enemy Health = Interface health.

To test the Interface method is working or not, we will now use some Debug Log functions to create that feedback.

In the Skeleton Damage method we are going use a Debug.Log (“Hit”); to check if the sword made contact.

In the Attack Class we are going to get the IDamageable script and give it a handle called “hit”. Then using the if statement to check if the component isn’t null, trigger the Damage method.

You can see on the bottom that the Damage script has been triggered.




A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.

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

Recommended from Medium

Why work as a handy-man helping me fix bugs?

“What would you like to do with your life?”

The 4 Best Free Coding Resources For Students

Cake v2.0.0 released

SAAS (Software as a Service) Platform Architecture

Why do I receive an error when attempting to connect to my Matrox

How to grow fast and teach faster — the opportunities of a modularized onboarding framework

GODE Announcement: GODE Listing Progress

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
Simon Truong

Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.

More from Medium

Learn to code by making 2 D space shooter.

Jumping in between two walls in Unity

2D Mobile: Designing Enemies Using Abstract Classes

Unity Animation System Part 2