AddComponent

Hi guys,

I’m a little confused by something. I was writing some scripts for my game as MonoBehavior when I decided to change the inheritance from MonoBehavior to Component but that didn’t work :cry:

So I try to look some informations on the class Component but I didn’t find anything to answer why it didn’t work since Component is the main class for evey component added to a gameobject.

If someone knows about it, I would be thankful :smile:

Why would you want to derive from Component instead of MonoBehaviour?

If I assume it has something to do with the title of the post, you know you can use MonoBehaviour-derived classes with AddComponent, right?

Yeah, I just wanted to try it but now I really don’t understand why Component doesn’t work. But I’m back to MonoBehavior inheritance.

Components are just handlers to the GameObject - they’re a totally different thing to Monobehaviours. It’s easy to assume that the MonoBehaviours inherit from Components, but that’s not the case.

The docs would indicate otherwise. MonoBehaviour inherits from Behaviour. Behaviour inherits from Component

Most of these classes are simply wrappers for objects on the C++ side. Without access to the C++ side of the engine there is no use case to inherit directly from Component.

Ok, I get it. Thanks :slight_smile: