- Home /
What is the most elegant solution to replace multiple inheritance for this?
Hi guys! I'm making my first game in Unity, a 2D Castle Defense game. I created a Creature class, which does not inherit from MonoBehaviour, for my monsters, where I have properties such as health points, armor, etc. I wanted to create a class inheriting from Creature for each monster type, with nothing different except for the constructor. The problem is, I wanted to attach these monster class scripts to my monster prefabs, but if they inherit from Creature, they won't be able to inherit from MonoBehaviour.
What is the best, most elegant solution for this? I think interfaces wouldn't help me, since I would still need to implement all of the exact same functions for the 10 or so attributes all monsters share in every single one of their scripts, so there would still be a lot of code.
Thanks a lot!
EDIT: I just thought of a possible solution. Would attaching the Creature script, instead of the specific monster script, to the prefab be a good idea? Then I could just have public variables rather than properties, which could then be set in the Inspector, replacing the job constructors would have had assigning the right amount of hp, armor, etc to each specific monster type.
Why not just make Creature inherit from $$anonymous$$onobehaviour..?
Thanks for your answer!
Yes, that is what I said I am doing after in the edit. Creature inherits from $$anonymous$$ono so its script can then be attached to the monster prefabs so that public variables can be set in the inspector, replacing the constructors' jobs in setting the attribute values. I think this is indeed the best solution!
I just realized something (which you may have tried to tell me and I didn't understand). If, say, "Orc" inherits from "Creature", and "Creature" inherits from "$$anonymous$$onoBehaviour", then Orc also inherits from $$anonymous$$onoBehaviour. I'm really stupid and I didn't realize that, thinking that in order for "Orc" to inherit from Creature, it wouldn't be able to inherit from $$anonymous$$ono, which would screw things up. Duh!
Answer by Meltdown · Jun 22, 2015 at 12:45 AM
Quite simple really, just make Creature inherit from Monobehaviour.