Call function of another actor's component

Hello, im making an atractor that pulls only 1 object and knows its movement speed, for that i have a class ARO (atractor) and a component class in an object calles AROTarget whcih has a public Test function and want to call it on ARO. i have this on ARO.h

protected:
	TSubclassOf<UAROTarget> target;
public:	
	UPROPERTY(EditAnywhere)
	AActor* affectedObject;

and on ARO.cpp

target = affectedObject->GetComponentByClass(UAROTarget::StaticClass());

but it doesnt let me compile nor call target->Test()

The function you are using is

UActorComponent * GetComponentByClass(TSubclassOf < UActorComponent > ComponentClass)

What you are getting is of type “UActorComponent”.
You have to cast that to “UAROTarget” to be able to use the methods of the class UAROTarget.

Not sure this is right, but I think something like this:

UActorComponent* pAC = GetComponentByClass(UAROTarget::StaticClass() );

if (pAC != nullptr)
{
  UAROTarget* pUT =  dynamic_cast<UAROTarget*> (pAC);
  if (pUT != nullptr)
  {
     pUT->Test();
  }
}

What I am REALLY unsure is, why this method exists. Can’t you get many responses?

I understand this: https://answers.unrealengine.com/questions/126431/how-to-use-getcomponentbyclass-findcomponentbyclas.html

Hope it helps.