Interfaces: IDamagable Interface in Unity

Niraj Karki
Nov 30, 2021

2D Mobile Game

Objective: Create a IDamageable interface so that player knows that the object that is being hit is damageable and make it take damage instead of taking reference to the gameobject script and act on it.

Create a new script and name it IDamageable as per the naming convention. Make it a interface instead of class and inside it define Health variable and Damage function that need to be implemented by all the gameobjects that can take damage.

Now define the interface to the damageable gameobject script with a comma behind the inherited script and implement the interface defined variable and function.

Now to use the interface to allow damage, check if the hit gameobject has IDamageable interface and set damage with hit.Damage()

Do this to all the damageable gameobject script and that's all you need to implement the IDamageable interface

--

--