Hey folks,
I’m new to Unity while having a bit of background in OOP and that led me to want to use the advantages of Interfaces and Base Classes. However I keep running into problems when I end up having to implement MonoBehavior, preventing me from using my own interfaces and base classes.
Let me quickly sketch out my scenario.
Character controller is the thing where everything is happening. it has the update method and such.
In this controller I have an instance of a player class that is implementing an ICharacter interface. That works great so far. I can run around and everything still works.
Then I want to collect some stuff on the ground, so I make a sphere with a script (with MonoBehavior) and a onTriggerEnter function that then should give the player one more sphere, but when I try to other.GetComponent().Inventory.pickupSpheres(1) it tells me Player needs to implement MonoBehavior which I can’t do because it’s already implementing something else.
I guess my question is: How do I plan out something from the start so that I never run into the problem of having to choose between MonoBehavior and my own base classes and interfaces? Or maybe I have the wrong approach?
Any tips would be appreciated <3
I’ll take a stab at it. I think you must have the wrong approach
It is C# so inheritance and interfaces work. Can you not inherit from MonoBehavior in your base class (if that is a requirement). Perhaps posting a concise concrete example of what doesn’t work would help. Post it as “code” please.
A class can only inherit from one other class, but it can implement any number of interfaces. There’s no reason your Player class can’t inherit from MonoBehavior (or indeed a custom class you define that derives from MonoBehavior) and still use whatever interfaces you’d like. Don’t forget you can also break up the functionality into multiple components to separate functionality where appropriate.
1 Like
oh I was under the impression that if you use a BaseClass you can’t implement any additional interfaces anymore.
After figuring out that MonoBehavior has to come first I got it working.
I thought it might be a good practice to reduce the number of classes that inherit from MonoBehavior for performance, but maybe that’s a topic for later…
Thanks Schneider21 and tleylan
1 Like
You can research the details later but for now rest assured there is no performance hit using class inheritance. MonoBehaviour is the base class of every Unity script.
2 Likes
Really curious about your background with OOP that gave you these ideas. That sounds like a sarcastic jab, but I legit mean it because I’ve really only ever worked with a few languages myself and am curious if it might actually be the case in some languages.
I agree it is a curious idea but I will take a guess that many people don’t quite understand the difference between a value and a reference. They may think that all the “stuff” is copied into the new class. Anyway that’s my guess and I would recommend to anyone learning C# in Unity to take a moment to understand C# as a language and computer languages in general. 
1 Like
I learned OOP in Java back in university. I’ve also used python. For Java I vaguely remember some performance / memory comparisons for inheritance and compositions. I’m not a programmer professionally, so I never cared that much.
Java, ugh
I coded in it but I never really liked Java.
In 2004 I did a course in Visual Basic up at Hallam Uni, got a City and Guilds from it for creating an app to randomly predict Lottery numbers, and it actually worked! I won a couple of tenners from it lol.