Modular Health System in Unity
Using interfaces you can easy track enemies or destructible objects for ontrigger events, for a simple interface we start with a new script, we call it IDamagable. we then change it from a public class that inherits from monobehaviour to a public interface that doesn’t inherit.
This turns it into a interface that we can inherit, we then add some inheritable structs that will use in all our objects that will be destroyed.
Then taking our enemy script we add our IDamagable script through inheritance. classes can inherit more then one interface so you can have a damage interface and a movement interface both inherited by the one class.
We then make sure those interface variable structs are in the inherited class, otherwise they will not compile, another useful component, so when we look to do damage to a object, we know that it will have the right function to take it.
Then when we want deal damage, instead of having to check each different script type that we want to be able to damage, we simply check to see if it has the IDamagable interface inherited, then since we know it will have the Damage method we can call that.