Call Function in unknown Class

Hello,
I’m trying to call a function from a class of an object. I have the Object but no clue to access the right Component.

A little Example:

public class Heal : Effect {
	public void ApplyEffect(GameObject user, int amount) {
		print("Healing " + user.name + " for " + amount);
	}
}

Thats the Method I want to Call from this:

public class ItemPotion : Item {
	public GameObject _effect;
	public int _value;
			
	public void Use(GameObject user) {
		Type component = ???
		component.ApplyEffect(user, 25);
	}
}

I cant call it with GetComponent() because it should work with any other class as well. When I call the Method, I know the Method is there because every class from Effect has this one, but I dont know the concrete Name of the Class.

Hope you can Help.

I know the Method is there because every class from Effect has this one

Are you saying that all of your effects inherit from Effect? Does Effect inherit from Component (or perhaps MonoBehaviour)? If so, you can still use calls like _effect.GetComponents<Effect>() to get a full list of them. You could write ApplyEffect() as a virtual method in Effect, and have each effect class override it.

If you’re having trouble understanding the above, you may want to read up on polymorphism, which is one of the most important principles in object-oriented programming. For starters, here’s an MSDN guide on override methods.

And, finally, if you can’t get that working, there is always SendMessage().