Hi all, I’m new in Unity.
I have a question about inheritance and prefab management.
In my game i want to use same character as player or enemy: for example I can choose a wizard as player and fight against orcs, that are enemies. In the following match, to fight the new villains, I want to change character and replace my wizard with an orc.
I created two classes: PlayerBehaviour and EnemyBehaviour.
Every character has his own class, like wizardBehaviour, orcBehaviour, goblinBehaviour…etc…, and has to inherit from PlayerBehaviour or EnemyBehaviour.
I created two versions of same script: wizardEnemy and wizardPlayer (that are identical) and in my resources folder I have two copies of the same character, one with attached the “player version” and the other one with “enemy version”. it seems to me that it is inefficient, so, is there another way to do it?
Is there a way to make my characterBehaviour inherit from EnemyBehaviour, if I want to use the object as an enemy, or PlayerBehaviour if I want to use the object as player without creating a lot of copies of the same thing?
There are several ways to deal with this, I’m not gonna post exact answer, but give you some direction for further research. What you’ve stumbled upon is called “Diamond problem in C#” and is usually solved by using interfaces instead of classes.
Another approach would be to rethink your whole structure and maybe use two classes instead of one, where one of them has an object of the other class inside. It really depends on methods and variables that you want to have there.
Hope that helps at least a bit in further research
thanks a lot for the ideas!
I have deepened the discourse of the interfaces but I don’t understand how I can use them to solve this problem. Since I can only declare methods, without being able to define them, I would still need two classes in which to define the methods, called “PlayerBehavior” and “EnemyBehavior”.
The classes of the various characters must have one of the two previous behaviors and add specific functions for each character, so using the interfaces I would still have to define the methods every time. I am not very familiar with interfaces and how they work, How could I use them in this case?