- Home /
How to use different types of scripts with an override function.
I have a Motor script that needs to call a Motion function from one of several types of Motion classes. So basically, one type of character might use the standard Motion script, while another character might use a SuperSpeed motion script that overrides the standard Motion class's main Motion function.
How can I tell my Motor class which type of Motion class to use?
Answer by Peter G · Jun 25, 2011 at 07:22 PM
It sounds like you want a virtual function.
public class Base {
public virtual void SomeMethod () {
Debug.Log("Base Method Called");
}
}
public class DerivedA {
public override void SomeMethod () {
Debug.Log("DerivedA Method Called");
}
}
public class DerivedB {
}
and here's an example of the implementation:
Base b = new Base();
DerivedA dA = new DerivedA();
DerivedB dB = new DerivedB();
b.SomeMethod();
dA.SomeMethod();
dB.SomeMethod();
Outputs
"BaseMethodCalled"
"DerivedA Method Called"
"BaseMethodCalled."
In a virtual
function, the child class can override the parent's implementation of the method. Otherwise the parent's method is called. This is helpful for things like this include places where you need a polymorphic behavior Another example would be something like Animal.Bark(). You want all animal to be able to bark but they all make a different sound so you would need to override the base classes behavior. Note Animal could likely be abstract, but just for examples sake.
Virtual functions shouldn't be confused with abstract
functions and/or classes. Abstract classes are classes that cannot be instanced and are designed to be inherited from by other classes. Abstract methods declared in abstract
classes must be overridden by the inheriting class similar to how interface methods must be given a body in the implementing class.
Virtual calls have a small performance decrease but it usually won't matter.
Hm... It looks like I'm on the right track then, but what I really need is some way to serialize said class. Then if I had say, a Robot using Robot$$anonymous$$otion ins$$anonymous$$d of standard $$anonymous$$otion, I could simply set his motion in the inspector.
@Nemo, seeing your post below, I think I understand what you want to do. Unfortunately I can't think of a way to tell Unity which child of $$anonymous$$otor to use with an inspected variable. Having said that, I would make a generic object moving script, and also attach the specific motor. So given you have a $$anonymous$$otor and a Robot$$anonymous$$otor. You would create a GameObject with 2 scripts on it: 1 to handle input and that passes that information onto the motor which controls the characters actual movement. So everyone would have this script:
class Input$$anonymous$$ovementHandler extends $$anonymous$$onoBehaviour {
private character$$anonymous$$otor : $$anonymous$$otor;
function Start () {
character$$anonymous$$otor = GetComponent.();
//This objects motor will be treated as a generic motor.
//That way you don't have to change this class at all per character.
}
function Update() {
character$$anonymous$$otor.$$anonymous$$ove(/pass input data here/);
}
}
and each class will get a specific motor script attached to it that overrides the default $$anonymous$$ove() command.
class Robot$$anonymous$$otor extends $$anonymous$$otor { override function $$anonymous$$ove () { //$$anonymous$$ove like a robot does. } }
Answer by BigBulle · Jun 26, 2011 at 08:02 AM
I would avoid where it's possible the use of inheritence in Unity because Unity uses the component pattern. Is it not possible for you to define public fields in your motion script that would change its behaviour (e.g. MAxAcceleration, MaxSpeed, ...)? There are several advantages in using public field:
they can be changed directly in the inspector even in play mode-
they are serialized with the scene
they can be changed dynamically by other scripts during the game-
I'm not sure I fully agree. Obviously any program$$anonymous$$g style can be done poorly, but I don't believe that using inheritance in Unity is usually a bad thing.
Without more information from Nemo, I wouldn't go for inheritance especially if he starts with scripting. But maybe Nemo should give more details on what he is doing and which features he is looking through inherictance.
Imagine you have a script with a public GameObject. You can drag your GameObject into the spot in the inspector, and it will function based on that object.
Ins$$anonymous$$d of GameObjects or Components though, I want to do it with a simple base class; essentially, I would want to drag the script onto the spot, and my system would call the function from whatever script is there. I hope that's a better explanation.
Your answer
Follow this Question
Related Questions
The variables and functions 2 Answers
Is there a way to override a function in each instance of the same script? 3 Answers
functions file and NullReferenceException: Object reference not set to an instance of an object 1 Answer
Javascript not being updated, variables being overridden, but C# is fine 1 Answer