IDamageable Interface in Unity- Game Dev Series 106

Objective: create an interface in Unity to handle damageable objects.

After setting an attackable sword, now we can try to attack the enemies. The concept of being attacked would be simple. We can create a method like “Damage()” to every enemy object and running the function after collided with sword.
However, imagine that you have tons of enemy object. You would never want to create Damage() one by one. We can create a type called “interface” to make function easier.

Creating an interface

When a class implements an interface, it must have to implement all the methods, properties from that interface. Interface just like a contract, it would force the class that implemented to use all the functions that the interface has.
And a class can implements multiple interface.

This box can both damageable and carryable.

Let’s create an interface to handle damage. Create a script called “IDamageable”. To identify the script type, using capital I stand for “interface”, and always using “able” at the end.

Interface could not declare variables, but use a property with getter and setter.
Let’s create an integer property to it.

And create another method to it.

Now we have a property and a method. Let’s implement this interface to an enemy.

Implementing an interface

When implementing an interface, simply add the name of interface after the semicolon of class name.

A class can only inheritance 1 abstract class, but implement multiple interface.

Once you implement IDamageable, it would show compiler error. That is because you also need to implement all the methods and properties from that interface.
Implementing Health and Damage() as public.

As same as Skeleton, you can implement IDamageable to any class that damageable, like a spider enemy, a box, or even Player.

Calling method from interface

Once we implemented IDamageable to Skeleton, we can test it by using sword to attack.
Open Attack script, call Damage() from IDamageable inside OnTriggerEnter2D().

Then back to Skeleton script and add one line to Damage().

Now we can play our game and try to hit Skeleton to check the result.

It works! We can use this interface to all the damageable objects.




A passionate gamer whose goal is to work in video game development.

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

Recommended from Medium

Boss Arriving Part-3: Health & Final Wave- Game Dev Series 54

Similar but Not the Same: You Still Need Developers, but Do You Need Coders?

Using AudioManager to Manage the Audio Effect- Game Dev Series 118

Paralympian @madison.____

The Ultimate Guide to GCP’s Logging Query Language

SES is not supported in your AWS region? Here’s what you can do

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
S.J. Jason Liu

S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.

More from Medium

Using GameDevHQ’s FileBase Asset Database For HQ Models

What is a Control Track in Timeline

Adding a burst laser powerup

Animating Sprites In Unity