Multiple Inheritance / Implementation alternative

Just because javascript doesn’t allow multiple inheritance doesn’t mean you can’t gain the same functionality via other means.

You would cleanly accomplish OP’s use-case in javascript using:

class Base extends MonoBehavior {
  // common behavior
}

function makeMaskableGraphic(someClass) {
  // add maskableGraphic functionality to `someClass` here
}

function isMaskableGraphic(instance) {
  // check if `instance` quacks like maskableGraphic
}

class SubClassA extends Base {
  // Unique functionality & implementation
}

class SubClassB extends Base {
  // Unique functionality & implementation
}

makeMaskableGraphic(SubClassB);

export { SubClassA, SubClassB, isMaskableGraphic };

Now if I could just find a non-boilerplate way to do the same in c# -.-

That’s not valid UnityScript, so it won’t run in Unity. UnityScript was* strongly typed the same way C# is.

The closest equivalent is to use composition. Fortunately you are in luck. Unity’s architecture is built around composition. You should be able to do the exact same thing with a couple of AddComponent and GetComponent calls.

  • I think its correct to refer to UnityScript in the past tense now.

Not to mention this was a necro post and TextMesh Pro is now part of Unity, so… things have changed since then. :smile:

3 Likes

I wish we would have multiple inheritance, it is so powerful when it is well done… And for the issue you describe in C++, you got virtual inheritance.
https://en.wikipedia.org/wiki/Virtual_inheritance

1 Like

S

Since you necroed a 5 year old thread, I’ll just throw this link out here:

To favour composition over inheritance may not be such a good/efficient practice in all cases, specially game programming.

Good point. I don’t know if the overhead is that large, though, but I’d love to see someone doing some performance tests. But there’s overhead in multiple inheritance too. The point is moot, though, since C# doesn’t support multiple inheritance.

1 Like