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?

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.