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 NinSonNar13 · Jun 26, 2013 at 08:34 PM · c#inheritancemethodssubclassing

Differentiating between inherited methods with inherited classes

I have a structure with 3 levels of inheritance, with methods that have parameters that have 2 levels of inheritance (if you don't count MonoBehaviour and up as extra levels).

It will be easier to explain the problem after I define the classes below:

Example Object Classes:

 public class Entity : MonoBehaviour {}
 
 public class SubEntity : Entity {
      public int foo;
      public InteractionScript interaction;
 }
 
 public class SubEntity2 : Entity {
      public string sfoo;
 }

Interaction structure:

 public class InteractionScript : MonoBehaviour {
         
    public virtual bool interacts(Entity interactor, Entity target, List<Entity> ents = null) {
       Debug.LogWarning ("Base Entity-Entity Interaction: Does Nothing.");
       Debug.LogWarning ("Interactor: " + interactor + " Target: " + target);
       return false;
    }

 }
     
 public class Interaction_SubEntity : InteractionScript {
         
    public virtual bool interacts(SubEntity sub1, Entity target, List<Entity> ents = null) {
       Debug.LogWarning ("Base SubEntity-Entity Interaction: Does Nothing.");
       Debug.LogWarning ("SubEntity: " + sub1 + " Target: " + target);
       return false;
    }
             
    public virtual bool interacts(SubEntity sub1, SubEntity2 sub2, List<Entity> ents = null) {
       Debug.LogWarning ("Base SubEntity-SubEntity2 Interaction: Does Nothing.");
       Debug.LogWarning ("SubEntity: " + sub1 + " SubEntity2: " + sub2);
       return false;
    }

 }
 
 public class Interaction_SubEntity_SpecificInteraction : Interaction_SubEntity {
 
    public override bool interacts(SubEntity sub1, Entity target, List<Entity> ents = null) {
       return true;
    }

 }

Now, while in the SubEntity class, I want to call interaction.interacts(SubEntity, Entity), but when I give it an interaction of type Interaction_SubEntity_SpecificInteraction (which I can in the inspector due to inheritance), instead of running that overridden version, it runs the base interacts(Entity, Entity).

Is the problem with my method of overriding methods, or that I am trying to differentiate between a subclass and it's parent class when the method name is the same? And, is there a way to fix this?

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 AntiLunchBox · Jun 26, 2013 at 08:45 PM

That function you are trying to call is a different function in that inherited class. You aren't using 'public override', you are using 'public virtual' again.

What you can do is:

 if(interaction is Interaction_SubEntity)
     ((Interaction_SubEntity)interaction).interacts(SubEntity, Entity);

Or just override those functions correctly.

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 NinSonNar13 · Jun 26, 2013 at 08:53 PM 0
Share

I realized it is different; it is supposed to be. I would like to run the function differently if it is given a subclass versus the parent.

That casting may work though. I had tried casting the parameter SubEntity, but I didn't try casting the interaction yet.

avatar image NinSonNar13 · Jun 26, 2013 at 09:05 PM 0
Share

Thank you for the help! The casting worked.

avatar image BlueRaja_2014 · Jun 27, 2013 at 05:32 AM 0
Share

That is terrible design - if you want to run a different function in the child class, give it a different name, don't shadow the method from the base-class.

avatar image NinSonNar13 · Jun 27, 2013 at 05:38 AM 0
Share

Actually, that's what I need. $$anonymous$$y program will be using interactions and behaviors from various scripts saved as prefabs so they may be attached in the inspector. Doing it that way allows me to test various behaviors and interactions I have made to find a combination that works best easily. Plus, I will be handing the project off to people who aren't necessarily programmers. To make sure they can work with it, the scripts need to work together, and therefore should have the same function name in each type of entity that it could be attached to.

avatar image
0

Answer by DaveA · Jun 26, 2013 at 08:39 PM

http://msdn.microsoft.com/en-us/library/ebca9ah3.aspx

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

18 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 avatar image

Related Questions

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Problem using variable of type CustomStat to indicate which player statistic to change 1 Answer

Question about Overriding 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