- Home /
Classes, MonoBehaviours C#
Okay, So I am new to unity programming.And I wanted to know the difference between, Advantages and dis-Advantages about Classes and MonoBehaviours.
Because I was trying to instantiate something in a class but the compiler says the method does not exist. Is it exclusive for MonoBehaviours?
Any help is appreciated :D
Thanks
Answer by whydoidoit · Feb 25, 2014 at 03:31 AM
You can Instantiate from anywhere but due to inheritance Instantiate requires no preamble inside a mono behaviour. In your own class you can Instantiate like this:
var instance = Object.Instantiate(somePrefab);
MonoBehaviours have the advantage of being serialized automatically by the system as are references to them on other MonoBehaviour classes that you might define in the scene view. They are pretty heavy, however, they also support all of the functions that indicate events within the Unity system such as Update, OnGUI and network connections.
The other "downside" of MonoBehaviours is that they must be attached to a GameObject, you can't just create one - you have to use GameObject.AddComponent which means they really are associated with a physical presence in your game world - that's often what you want, but if you are looking to describe some more abstract concept you'd be better off with normal classes.
To expand a bit. class
is a C# language construct. $$anonymous$$onoBehaviour
is such a class and it is a specific Unity implementation. You do not have to use $$anonymous$$onoBehaviour
s for your classes. But you do have to use them, if you want to attach it to a GameObject
. $$anonymous$$onoBehaviour
simply enables you direct access to the attached game object's properties, methods and events. The only difference is that you cannot construct Unity Object
-based classes, you have to Instantiate
/AddComponent
them because they always come attached with some game object.
Answer by getyour411 · Feb 25, 2014 at 02:59 AM
Mono - Awake(), Start(), Update(), etc and attached to GameObject. Classes that doesn't derive from Mono - not.
Your answer
Follow this Question
Related Questions
Making a static class derive from MonoBehaviour in C# 3 Answers
Distribute terrain in zones 3 Answers
Using a non-monobehaviour class file 1 Answer
Question about classes on C# 1 Answer
Multiple Cars not working 1 Answer