Interfaces

Al Heck
2 min readOct 22, 2021

Often, these are declared with an “I” at the front and a “able” at the end. It’s written like a class minus the parameters.

You cannot have fields. public int health will error out. But you can set it with a property.

You can add methods, but you can’t add any info inside.

I can create 2 game objects in scene (enemy and player) and I can have their scripts inherit IDamagable. For this to work, I must also add the property of health and the function of damage with the parameters.

This can be a little exhausting, but we can have Visual Studio add these automatically. If you right click and choose Quick actions and refactoring, it will auto implement those for you.

With that set, I can create a health system. Each game object has individual health and damage functions. I can pass in the parameter and run the function.

You can also add multiple interfaces — highly valuable since you can’t add multiple classes!

--

--

Al Heck

Unity Developer and Software Engineer who loves making games with GameDevHQ