Unity C# Interface Beginner's Guide

An interface in C# is a contract that defines a set of methods, properties, and events that a class must implement. It acts as a blueprint for implementing functionality in multiple classes. Interfaces provide a way to achieve polymorphism, allowing different classes to share common behaviors.

Unity C# Interface

To use interfaces in Unity, follow the steps below:

  • Create a new script and name it anything (in my case I'll name it InterfaceContainer)
  • Remove everything inside it then paste the code below:
public interface IEntity
{
    void Initialize(); //Function without any arguments
    float health { get; set; } //A variable
    void ApplyDamage(float points); //Function with one argument
}

The interface is called IEntity (Note: The capital i at the start is not necessary, but for convenience, name all your interfaces with 'I' at the start, that way you would know when the class uses an interface).

To use the interface in a C# script, follow the steps below:

  • Create a new script and name it anything (in my case I'll name it EntityScript)
  • Add interface name after the MonoBehaviour, which is IEntity in this case (separated by a comma)
public class EntityScript : MonoBehaviour, IEntity

You'll notice that the script gives an error, that's because the interface methods are not implemented. So let's implement IEntity methods:

using UnityEngine;

public class EntityScript : MonoBehaviour, IEntity
{

    public float health { get; set; }

    public void Initialize()
    {
        health = 100;
    }

    public void ApplyDamage(float points)
    {
        health -= points;
    }
}

The interface methods are now implemented.

How do interfaces simplify the programming workflow?

The main advantage of C# interfaces is that they can be used by multiple classes, so instead of calling GetComponent for each script, you can get all the script references by using the interface name.

Use the C# interfaces when you need to implement the same methods in more than one script, or when you need to reference an unknown class in the same context.

Check the example below:

        //Get the script that uses IEntity interface
        IEntity interfaceEntity = gameObject.GetComponent<IEntity>();
        interfaceEntity.Initialize(); //Initializing the entity
        interfaceEntity.ApplyDamage(10); //Applying the damage
        interfaceEntity.health += 10; //Healing the entity

The script above gets a component with an IEntity interface, then calls its methods.

Bonus

Interfaces can also accept custom data types, for example:

public interface IHealable<T>
{
    void Heal(T type);
}

The data type is then provided when implementing the Interface in a Class (It can be a standard type such as float or int, or a more complex type such as a Class or even another Interface):

using UnityEngine;

public class EntityScript : MonoBehaviour, IEntity, IHealable<int>
{

    public float health { get; set; }

    public void Initialize()
    {
        //health = 100;
        Heal(100);
    }

    public void ApplyDamage(float points)
    {
        health -= points;
    }

    public void Heal(int points)
    {
        health = points;
    }
}
Suggested Articles
Introduction to State Machine in Unity
A Guide to Scene Loading in Unity
How to Become a Better Programmer in Unity
Implementing Object Pooling in Unity
Implementing VR Headset Control in Unity
A Guide to Integrating Nintendo Controller with Unity
Creating a Pac-Man-Inspired Game in Unity