Is it possible to calling inherited component's overrided function through parent's component?

Hi there, i was thinking about making Interaction with Object and Player.

I made a State Machine, and when Player enters Object’s collider, collider’s trigger sends it’s own gameobject to Player’s State_Interaction component.

when Player’s State_Interaction catch it’s Interaction animation’s end, it calls received gameobject’s interaction result function through gameobject’s component.

but to make Interactable Objects widely, such as Switch(call it’s animation) and Obstacle(destroy gameobject), I thought I should use inheritance for using just one method.

So I guess, calling inherited component’s overrided function through parent’s component will be perfect for me. is it possible?

Long story short, Such as;

abstract class Object & virtual void Interaction();

class Object_Switch : Object & void Interaction() { return A; } / class Object_Obstacle: Object & void Interaction() { return B; }

when calling each classes Interaction(); function through gameobject.getcomponent().Interaction(); method → can result be different?

I’m sorry for took your time so long & my poor English. thanks!

Your example isn’t specific enough to give a definite answer.

The method that gets called (and subsequently the result) depends on the actual type of the object you call Interaction() on.

Object o1 = "some string";
Object o2 = 10;

Debug.Log(o1.ToString());
Debug.Log(o2.ToString());

Variables o1 and o2 are both objects, but you can store any object in them. Which override version of ToString() gets called doesn’t depend on the type of the storing variable, it depends on the actual type underneath.

The same way if you call

YourClass o1 = GetComponent<YourClass>();
o1.Interaction();

Which override version of Interaction() gets called depends on which script was actually attached to the gameObject.

Calling GetComponent<YourClass>(); will return any script that derives from YourClass that is attached to said gameObject.

If this is what your example was depicting, the answer is yes.