Interface


What is an Interface?

Imagine you’re designing a system where different objects — like players, enemies, turrets, and traps — all need to take damage. Some of them have health bars, others explode, others turn off or disappear. Their reaction to taking damage might be totally different… but the fact that they can be damaged is something they all share.

You could try to write different code for every object, or hard-code conditions like “if this is a Player, then do X… else if this is a Turret, do Y…” — but that quickly becomes a tangled mess.

Enter interfaces.

An interface is like a contract that says, “Any class that agrees to follow me must implement these specific actions.” It doesn’t say how the actions should work — only what the actions must be called and what inputs they should take.

For example, you could define an interface called IDamageable that says: “Anything that wants to be damageable must have a method called TakeDamage(int amount).” That’s it. It doesn’t care how health is reduced, how the object responds, or even if there’s a health bar at all. It just cares that the method exists.

Then any class — player, enemy, boss, object — can implement this interface. And now your damage system can say, “If this thing implements IDamageable, I know I can safely call TakeDamage() on it — no matter what kind of object it is.”

  • Interfaces define the shape of behavior
  • They allow different types to behave the same way
  • They remove the need to know what something is–only what it can do
  • They help you write cleaner, more flexible, reusable code


Interface Anatomy in C#

Step 1: Define the Interface

C#
public interface IDamageable
{
    void TakeDamage(int amount);
}


Step 2: Implement It in a Class

C#
public class Enemy : MonoBehaviour, IDamageable
{
    public int health = 100;

    public void TakeDamage(int amount)
    {
        health -= amount;
        Debug.Log("Enemy took damage! Remaining HP: " + health);
    }
}


Step 3: Use It in Code

C#
void OnTriggerEnter(Collider other)
{
    IDamageable damageable = other.GetComponent<IDamageable>();
    if (damageable != null)
    {
        damageable.TakeDamage(25);
    }
}


Interface Breakdown

2. Public interface IDamageable

  • Declares a new interface
  • By convention, interface names start with I (e.g., IInteractable, IDraggable, ICollectible)
  • Does not inherit from MonoBehaviour or anything else

2. Void TakeDamage(int amount);

  • Defines a method signature only — no logic
  • This is the “contract”: any class using this interface must implement this exact method with this exact structure
  • You can’t add logic or default behavior inside an interface

3. Class Enemy: MonoBehaviour, IDamageable

  • The class implements the interface
  • The : indicates inheritance/implementation
  • You can implement multiple interfaces with commas: : IDamageable, IHealable, IExplodable

4. Implementing the Method

C#
public void TakeDamage(int amount) { ... }
  • Now the Enemy class is officially “damageable” and fulfills the contract
  • This logic is specific to Enemy — other classes like Player or Turret can implement TakeDamage() differently, but still share the same interface

5. Using the interface for flexible logic

C#
IDamageable d = hitObject.GetComponent<IDamageable>();
if (d != null)
    d.TakeDamage(10);

Now your code:

  • Doesn’t care if it’s a Player, Enemy, Trap, Crate, Vehicle, or Alien Banana
  • Only cares: can this thing be damaged?

This is the magic of interfaces–they decouple your logic from specific types.

Key Concepts

Interface: A list of method signatures that a class agrees to implement

Contract: If you implement the interface, you must provide code for all of its methods

Implementation: A class can implement as many interfaces as needed

No logic: Interfaces only define what must exist, not how it works

Polymorphism: Interfaces let you treat many different objects the same way


Unity Common Interface Examples

IDamageable–Can take Damage
ICollectible–Can be picked up
IInteractable–Can be interacted with (NPC, door, terminal)
IHealable–Can be healed
ICraftable–Can be combined with other items
ISaveable–Can be serialized and saved
IAttackable–Can be targeted or attacked

Summary:

Interfaces are like invisible agreements. They say, “I don’t care what you are, I just need to know you can do this thing.” They help your code be more flexible, more scalable, and more independent of specific object types.

Instead of checking what kind of object something is, interfaces let you just ask, “Can it take damage? Can it interact? Can it be used?” If the answer is yes, you know what method you can safely call.