First off, I’m a bit of a unity noob and still learning a lot of the basics.
Up till now I’ve used inheritance for my classes, and I’ve recently learned of interfaces. However, no matter how many posts I read I cannot see their proper use. I just don’t understand how they are necessary, and I think this is because I don’t really know their power.
In my project I have now split my used to be “CharacterBaseClass” into various components (HP, Atk, Atk Speed etc. all holding variables, and methods). I don’t understand how interfaces could be of any help to my system, yet apparently they work wonders with a component based approach. It’s my understanding that if I make an interface IShootable, for instance, I can declare methods within it that must be called in any classes that ‘inherit’ said interface. So let’s say my IShootable contains a Shoot () method… Now any class that inherits this interface has to include this method, but that method has individual actions for each separate class is attached to. I fail to see how this is useful, as it is surely just extra lines of unnecessary code. I understand the problems that you can run into using class inheritance with conflicting inheritance types etc. but then what is the difference between: writing a Shoot () method in every Class that uses it, and: writing an interface including Shoot () and then attaching this interface to each component that can shoot, and then writing the code for the method anyway.
What am I missing here? Can someone please enlighten me as to how interfaces can properly be used in a component based project search as mine? Please try to simplify as much as possible as I have read various threads and followed tutorials and they still haven’t cleared it up for me.
Many thanks in advance