Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Zankaster · Aug 22, 2014 at 04:17 PM · override

How to call an overriden method from parent class?

Hello, I have a basic question about inheritance. Let's say for example I have a father class named Character, and two children classes called Player and Enemy:

  public class Character : MonoBehaviour {
     
         // Use this for initialization
         void Start () {
     
                 Hello ();
         }
         
         // Update is called once per frame
         void Update () {
             
     
         }
     
         public virtual void Hello()
         {
             print ("Character");
         }
     }

I have this public virtual method I need to customize in the child classes:

 public class Player : Character {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     public override void Hello()
     {
         print ("Player");
     }
 }

And similarly for the Enemy class. I created 3 gameobjects and attached one of the 3 scripts to each one, and only the Character class gave me an output. The one above is just an example, what I would like to do is to call a series of instructions in the Character class (defined in the Character class), and add a custom behavior to some of them in the child classes. Is it the right approach to use hierarchy? Or should I use interfaces?

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

Answer by Landern · Aug 22, 2014 at 04:19 PM

base.Hello(); // refers to the inherited class hello method

this.Hello(); // refers to this instance and the hello method

Comment
Add comment · Show 4 · 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 Zankaster · Aug 22, 2014 at 07:08 PM 0
Share

Ok, but what I would like to do is to call the child Hello() method from the parent, I don't even know if it's possible honestly that's why I'm asking if it is or if I should resort to other options.

avatar image Landern · Aug 22, 2014 at 07:12 PM 0
Share

Suppose you could if you either registered the event or delegate in the constructor(or at some other point), otherwise the super class isn't aware through normal means. You could use interfaces or casting if your passing around the base class which is actually a child class.

See: http://stackoverflow.com/questions/6343301/up-casting-c-sharp

avatar image Zankaster · Aug 22, 2014 at 07:40 PM 0
Share

I wanted to write the AI for the characters in the parent so to write it only once, but it seems it's much more complicated than just rewriting some code in each of the children, so I think I'll go that way since I guess it's the more "natural" way to do this, thank you very much for the help ;)

avatar image Kiwasi · Aug 22, 2014 at 07:46 PM 0
Share

Its not possible with normal architecture to call a child method from within a parent class. And if you think about it, it doesn't make much sense, because there is no guarantee that there actually is a child class, or there are not four chained child classes.

On the other hand if you do Character.Hello(); it calls the most overridden method. In this case the one on player.

avatar image
0

Answer by shadoath · Sep 20, 2020 at 02:50 PM

I recently ran into this problem as well. Your approach to using the hierarchy is correct here, given their will be multiple enemies. Interfaces would best be used when you only want one object to exist that you can reference across the game.

One change I would make is to remove anything from the vitural method and always override it.

Comment
Add comment · Show 1 · 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 StuteJ · Jan 25, 2021 at 05:36 PM 0
Share

I would actually add a warning to the virtual $$anonymous$$ethod, so that if it is not overridden that the Dev will be notified, instead of nothing happening at all.

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

Override windows onaly allowing three buttons being pressed 1 Answer

Working with Facial Animations in Mecanim 3 Answers

Sprite.OverrideGeometry() doesn't work? 1 Answer

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

Possible to make "override all" button on prefab instances ignore certain values (like it does for position)? 0 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