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

Microsoft Continues to Embrace the Open-Source Community

Microsoft ❤ Linux

Raise your hand if you want to quit drinking coffee. Why, though???

The Secret to Speed and Stability

How-to: Easily Automate your Linux System Updates

git-lfs: How to work offline? How to archive/bundle a lfs repo?

Understand Python Decorator in Easy Way

How Microsoft Has Impressed

Understanding the risks of Cloud Computing — Pt 1

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

Day 105: Working with Animation Events in Unity

HitBox Attack System in Unity

Adding Game Over UI elements

Creating Elevator in Unity — Part 3: Moving Elevator.