Does my base class that inherits from MonoBehaviour needs to be attached to an gameobject within a scene?
Hey guys! I've worked in C# in Unity for few months now, but without using inheritance at all. :D Today I decided to start learning it, I understand a lot now, but one simple (?) thing I don't (before I start implementing and refactoring my code), it's "Does my base class that inherits from MonoBehaviour needs to be attached to an gameobject within a scene?". I know that base class contains all required variables,methods for other scripts that inherits from it, but, if answer to my question is Yes, then where I should attach it?
I hope someone can answer this! BTW Thanks to Unity Community for helping me a lot! :D
Answer by NoseKills · Jul 03, 2016 at 04:33 PM
It's sometimes pretty hard to understand what people learning inheritance are actually asking because it's so easy to misunderstand inheritance in such complicated ways...
So if you make classes
class Base : MonoBehaviour {
public int baseInt;
}
class Extend : Base {
public int extendInt;
}
what i think you are asking is, do you need to attach both of these scripts to gameObjects to take advantage of inheritance.
The answer is no.
That's because in a sense there are no 'two scripts' here. When you extend a class, it basically means everything declared in the base class gets merged into the extending class when you create an instance of the extending class.
You can almost think of inheritance as a text editing feature instead of anything more complicated.
If you now drag the Extend script into a gameObject you effectively have a script like this on the object
class Extend : MonoBehaviour {
public int extendInt;
public int baseInt;
}
Related to this, another thing that confuses peolple is that the instances of base script and instances of extending scripts are not magically connected in any way.
If you drag the Base class onto one object and Extend class to another object, then run the game, the scripts are separate instances despite of inheritance. If you change baseInt
on one object, that variable doesn't change in the othe objects because they are separate instances. Each of them has their own baseInt
despite inheritance.
If you make a base class 'Mammal' and put all the 100 methods and variables required to describe a mammal there and make an extending class 'Horse', that's just a way to avoid having to write those 100 methods and variables again when you make the 'Cow' class. When you make a game with those classes, you probably only attach 10 Cow and 10 Horse scripts to different objects and they will each have their own 100 mammal variables. You don't need to have separate Mammal scripts on objects in the scene to make things work. The mammal stuff is already in each of the cow and horse objects because of inheritance.
So I can leave my base class in my assets folder and just use it to inherit data?
Yes. All of the C# classes you make are a part of the C# solution of the Unity project and get included in the builds you make. What you drag'n'drop and where in Unity editor is a separate thing and doesn't really concern the C# project
This is actually a great initial explanation of inheritance! Will be using this when I $$anonymous$$ch from now on!