- Home /
Component Class Hierarchy
I've created a hierarchy of UnityScript classes.
SuperClass
SubClassA extends SuperClass
SubClassB extends SuperClass
These are currently stand-alone classes that I instate in code. I'd now like to map them so each one is a Component.
The problems seems to be that they all need to inherit directly from MonoBehavior.
Is it possible for my SuperClass to inherit from MonoBehavior, and the subclasses to inherit from the SuperClass ?
If it's not possible in UnityScript, would this be possible in C# ?
Answer by Bunny83 · Mar 25, 2012 at 02:40 AM
It should also be possible in UnityScript that way:
class SuperClass extends MonoBehaviour
{
}
class SubClassA extends SuperClass
{
}
class SubClassB extends SuperClass
{
}
Note that every class have to be in it's own file and the file name has to match the class name. Otherwise you can't add the scripts to a GameObject.
I use C# exclusively because i don't like the JS-syntax ;) Also C# allows to inherit from one base-class and from additional interfaces whereas UnityScript only allows one ancestor. Also i don't have a clue of the virtual / override syntax in UnityScript which is actually one of the most important things when you use inheritance.
In C# you would do something like that:
public class SuperClass : MonoBehavior
{
protected virtual void Update()
{
}
}
public class SubClass : SuperClass
{
protected override void Update()
{
base.Update(); // call the inherited function Update() from SuperClass
}
}
Answer by cowlinator · Mar 25, 2012 at 01:59 AM
Yes it is possible in c#
SuperClass : MonoBehavior
{
...
}
SubClass : SuperClass
{
void Update()
{...}
//etc.
}