How would I get around multiple inheritance for this instance?

Hey all, I’ve been really digging deep into the programming I’ll need and I’m coming up empty on some important features.

Namely, I need (essentially and effectively) 2 classes for a character that command completely different things.

Class A should command stats and magic.
Class B should command physical skills and weapons/armor.

There is a story reason for this, in case you are curious as to why I would do this, but I digress.

I looked around and while you can definitely let a script borrow from a few (via interface) with a comma in the x : y of the class, you can’t then refer to that script in a separate script cleanly. In other words, multiple inheritance can’t really happen (as many forum posts alerted me to once this happened to me).

So if I need a script to refer to these two individual parts… what exactly would be the best way to go about that? I’m incredibly stumped on how to join those two aspects in a coding sense.

Favor composition over inheritance. Have two separate scripts, lets call them Magic and Physical. Then to refer to both classes you just need to get a reference to each component on the same object:

GameObject character = /*Get the character*/;
Magic magicComp = character.GetComponent<Magic>();
Physical physComp = character.GetComponent<Physical>();

Composition is definitely going to be the easiest way to accomplish this, but you can also do something like this:

public interface IMagic
{ /*magic stuff*/ }

public interface IPhysical
{ /*physical stuff*/ }

public interface IMagicAndPhysical : IMagic, IPhysical
{}

Alternatively, if you really wanted to have only one component class with multiple interfaces, but still refer to the interfaces separately:

public class Character : MonoBehaviour, IMagic, IPhysical
{}


//Then in some other code

GameObject charObj = /*get object*/;
IMagic magic = charObj.GetComponent<IMagic>();
IPhysical phys = charObj.GetComponent<IPhysical>();
//or if you wanted access to all the character stuff
Character char = charObj.GetComponent<Character>();

In the last example, magic, phys, and char are actually all the same object, but ‘restricted’ to different portions of the object.

I suggest you go with the first solution (composition). It is the easiest to work with. You say there is a story reason for why you would want multiple inheritance, but there is no reason I can think of why your code has to mirror your story. Your story can make it seem like a character is two different things at the same time, but your code does not have to handle that with multiple inheritance (which models an object being two things at once).