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 /
avatar image
0
Question by Baal_Rog · May 23, 2013 at 06:52 AM · c#inheritancepolymorphism

Calling methods on inherited classes in C#

Hey all, I'm still trying to wrap my brain around polymorphism, inheritance, and all this OOP nonsense. I come from a procedural scripting background so my gut instincts are all wrong when trying to do some actual programming.

The framework I'm attempting is sort of a hierarchy. The Entity class is like the nervous system, and gives the other components easy access to each other, as well as telling the components when to act so I can control the order in which things happen. There's Movement and Brain (controls for the player, AI for enemies) base classes, and inherited classes created (Move_Human, Brain_Player) under those.

My Entity class has no subclasses, and it's variables are typed as the Base classes for movement and brain. When the entity calls a method from an attached Move_Human component, The Movement script is what actually runs it's method.

How the heck do I run the inherited methods rather than the base methods?

The example code below prints out "Movement says HI!". There's a single game object with an Entity and a Move_Human component applied.


 public class Entity : MonoBehaviour {
     
     public Movement myMove;
     
     // Use this for initialization
     void Start () {
         myMove = gameObject.GetComponent<Movement>();
         myMove.sayHi();
     
     }
 
 }



 public class Movement : MonoBehaviour {
 
     public void sayHi(){
          Debug.Log("Movement says HI!");    
     }
 
 }

 

 public class Move_Human : Movement {
     
     void sayHi(){
          Debug.Log("Move_Human says HI!");    
     }
 
 }



Also, can anyone recommend resources to untrain my brain of it's procedural ways?

Thanks -

Chris Luckenbach

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
0
Best Answer

Answer by DaveA · May 23, 2013 at 06:54 AM

Get familiar with the 'virtual' keyword: http://msdn.microsoft.com/en-us/library/9fkccyh4.aspx

Basically, your base class declares a method as virtual, then calling it will call the derived method instead. Basically.

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 Baal_Rog · May 23, 2013 at 05:48 PM 0
Share

Thanks for the quick responses.

I've added virtual to the base class, but still get exactly the same results

 public class $$anonymous$$ovement : $$anonymous$$onoBehaviour {
 
     public virtual void sayHi(){

I've tried adding the new keyword to $$anonymous$$ove_Human.sayHi(), but it makes no difference.

avatar image Hoeloe · May 23, 2013 at 06:58 PM 1
Share

'virtual' should be on the parent's definition. This is used to mark the method to use dynamic polymorphism, allowing overridden definitions to be run on the new object. On the inherited class, you need to make the method with 'override', to tell the compiler that you are treating it as the same method.

avatar image
0

Answer by Ashkan_gc · May 23, 2013 at 07:35 AM

Well you should define SayHigh as virtual and then use override keyword in Move_human.

Some points, For keeping with standards use caps letters instead of underscores (_). soe MoveHuman instead of Move_human It's better to name it HumanMovement however.

I recommend the book code complete for general great guidelines about coding and for learning object oriented C# programming i recommend C# how to program if you are a beginner and programming C# by j.liberty if you are advanced (which it doesn't seem and you say you are comming from a procedural background)

Comment
Add comment · 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

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

How to change variables from another script? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Derived Class Fields 3 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