- Home /
Can a Monobehaviour extend a class?
As the title states I am looking to rewrite code here as much as possible and in some cases I want a few different methods and a change in a few variables but I want the bulk of the work to be the same. It took me a while to work out what I wanted to do / how to ask the question but I think the above is the most simple version.
In c# can I create a normal class structure and then get one of my Monobehaviour scripts attached to an object to extend that class and thus get all it's functionailty? Thanks!
does this sort of answer help ?
http://answers.unity3d.com/questions/430582/extending-a-singleton-base-class-ujs-.html
suggest ou search for 100s of questions on the topic you ask ...
Answer by EliteMossy · Apr 07, 2013 at 07:23 AM
Say you have a class called Agent you can derive from this class and create 2 classes called AgentFast and AgentSlow.
Obviously you want to attach AgentFast and AgentSlow to a GameObject. Well to do that you can do:
public class Agent : MonoBehaviour{
}
then to derive from it you do
public class AgentFast : Agent {
}
public class AgentSlow : Agent {
}
And you get full access to the MonoBehaviour in the base class.
Ahh, O$$anonymous$$ thanks that makes more sense. I'll muck around with it tonight and see if I can't get some basic functionality working.
Just a note, if you are using Awake, Start, Update etc in the base class, then you decide to use the override keyword ins$$anonymous$$d of new, you will need to do base.Awake or base.Start etc to process the base
Your answer
Follow this Question
Related Questions
Is it possible for a non-MonoBehaviour class to use built-in functions? 1 Answer
new Monobehaviour as variable returns warning... 1 Answer
How do I use "helper classes" in C# and have them show up in the Inspector? 1 Answer
Monobehavior class variable become null after assigning value when instantiated 1 Answer
Can I call a class's method which inherit monobehaviour by a normal class? 0 Answers