- Home /
why non-Monobehaviour classes cannot be attached as a component in Unity3D?
Classes that are not inheriting from Monobehaviours cannot be attached to a Gameobject as a component. Why is it? What is Monobehaviour exactly?
Answer by Mouton · Sep 26, 2019 at 05:24 PM
MonoBehaviour
inherits from the Behaviour
class which inherits from the Component
class to extend its functionalities.
Component
It is the base class for everything that can be attached to a GameObject.
It implements component related stuff such as GetComponent
methods.
Behaviour
A component that can be enabled or disabled, it does not implement anything else.
MonoBehaviour
It is the base class for every scripts. It implements some basic method, such as Invoke and Coroutine handlers. It also implements all the messages (event methods) Unity calls internally.
You can create a class which inherit from Component
, but it won't receive Start
, Update
, and such messages and you won't be able to enable/disable it. By inheriting from Behaviour
, you can enable/disable your component, yet it does not have much effect since this is mostly useful for the event methods. It is very uncommon to inherit directly from these classes but you can do it if you need an object which just need to use GetComponent and such methods.
@$$anonymous$$outon Thanks a lot for the answer :-)
I have a doubt. Like what is the software architecture used to build Unity3d.
I'm not sure what you question is about or why this is relevant. However Unity is actually written in C++. They only use C# / .NET for the managed scripting side. And no, you can not and should not derive any of your classes from Component or Behaviour. Only $$anonymous$$onoBehaviour (a behaviour written in $$anonymous$$ono / C#) can be used as base class for custom components. Note that every UnityEngine.Object derived class has a C++ counterpart. $$anonymous$$ost of the functionality is actually implemented on the native C++ side. The C# code consists mainly of wrapper classes.
thanks a lot @Bunny83 :-) ... this is very informative.
All I was trying to ask was ... like does unity3d is built on ECS architecture. I know that they have introduced ECS in Unity for better performance of the games built with Unity. So, my question is that is Unity3D is built on ECS architecture (software architecture)?
Your answer
Follow this Question
Related Questions
Accessing a MonoBehaviour List within a Non-MonoBehaviour Script 2 Answers
Is it possible for a non-MonoBehaviour class to use built-in functions? 1 Answer
MonoBehaviour methods in non-MonoBehaviour class 1 Answer
How to use GetComponent in Non-Monobehaviour script? 2 Answers
How do I detect that MonoBehaviour is modified in the Editor? 2 Answers