C# Interfaces In Unity

Ben Pielstick
1 min readJun 7, 2021

--

Interfaces can be a tricky subject for new programmers, but once you understand their basic concept, you can start thinking of lots of potential uses for them. The way I think of an Interface is like an abstract class with no implementation. When you are using an abstract class for inheritance you will often want to implement some code in its methods in order to share that code across all the children that inherit it. Interfaces don’t provide that much functionality, but since C# doesn’t support multiple inheritance, an interface can be a good workaround since you can implement multiple interfaces within the same class.

While Interfaces don’t provide as much functionality as inheritance, they are still useful anywhere you want to ensure similar functionality across a set of different game objects in Unity. For example, if you wanted to deal damage only to things that are damageable, you might utilize an interface like this:

To be damageable, we need a Health property and a Damage method
When our attack trigger happens, we make sure what we hit implements the IDamageable interface, and if so we know it has a Damage method we can call.

--

--

Ben Pielstick
Ben Pielstick

Written by Ben Pielstick

Game developer with over 12 years of experience, mainly focused on MMOs.

No responses yet