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
2
Question by GarbagePile · Jun 03, 2017 at 04:01 PM · inheritancefunctionsoverridemethodsvirtual functions

Can't Override Virtual Method In Inherited Class

I am currently on part 6 of the 2D Platformer Character Controller Live Session. I have a PhysicsObject class that inherits from MonoBehaviour, and a Player class that inherits from the PhysicsObject class, just like in the tutorial.

I have the following in PhysicsObject.cs:

 void Update () {
         targetVelocity = Vector2.zero;
         ComputeVelocity();
     }
 
     protected virtual void ComputeVelocity()
     {
 
     }

and the following in my Player.cs:

 protected override void ComputeVelocity()
     {
         // Handle horizontal movement
         Vector2 move = Vector2.zero;
         move.x = Input.GetAxis("Horizontal");
         targetVelocity = move * maxSpeed;
 
         // Handle jump
         if (Input.GetButtonDown("Jump") && grounded)
         {
             velocity.y = jumpSpeed;
         }
         else if(Input.GetButtonUp("Jump"))
         {
             if (velocity.y > 0)
                 velocity.y *= .5f;
         }
     }

Player.cs's ComputeVelocity() should override PhysicsObject.cs's ComputeVelocity(), but this is not happening. ComputeVelocity() is being called by PhysicsObject, but it is the blank version in PhysicsObject.cs, not the overwritten version in Player.cs. I am able to use the function properly by calling it from Update() in Player.cs, but I cannot figure out why the override is not occurring.

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

1 Reply

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

Answer by Bunny83 · Jun 03, 2017 at 04:08 PM

I can guarantee that the code you provided does work, even without testing it. If it doesn't work for you, you probably have missed something about the setup. First make sure you only attach your Player script to your player object and not a "PhysicsObject" script.

I am able to use the function properly by calling it from Update() in Player.cs

So does that mean you implemented an Update method inside your Player script? In that case the Update in the base class will get hidden. A class can only have one Update method. If you want to make Update extendable in a child class it should be virtual in the base class and properly overridden in the child class. Also make sure you call base.Update() inside your Player's Update if you want to keep that functionality.

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 GarbagePile · Jun 06, 2017 at 04:29 PM 0
Share

Yep, this was the issue. I made both Update functions virtual and added base.Update() to the Player's Update function so that both of them would be called (the physics are of course handled in PhysicsObject.cs while I have code for firing bullets in Player.cs's Update function). Thank you so much!

avatar image Maska77 · Jan 03, 2018 at 08:25 AM 0
Share

Thanks bunny, I was looking forever for an answer like this. I didn't realize the base class wasn't allowed to have a Start/Update function if the class I'm inheriting from already does.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Theory: Methods and Functions 1 Answer

Weird behaviour of an editor extension that sets variables on other scripts. 1 Answer

Animation Event (Function not supported) for a public overridden function 0 Answers

How do I override objects that aren't in the Hierarchy? 0 Answers

Is it worth making tons of similiar methods or create big one that connects things together? 2 Answers


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