Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nemox · Jun 25, 2011 at 06:50 PM · variablefunctionoverride

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nemox · Jun 25, 2011 at 08:09 PM 0
Share

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.

avatar image Peter G · Jun 26, 2011 at 08:47 PM 0
Share

@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. } }

avatar image
0

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-

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Peter G · Jun 26, 2011 at 04:50 PM 0
Share

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.

avatar image BigBulle · Jun 26, 2011 at 06:21 PM 0
Share

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.

avatar image Nemox · Jun 26, 2011 at 07:52 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

Calling a Function from other script ( C# ) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges