- Home /
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?
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.
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.
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104932im_/https://answers.unity.com/themes/thub/images/avi.jpg)