Getting location from a UActorComponent

So as the question says. I know it not really possible but I have an array of UActorComponents from using getCompoentByClass and I need to be able to get the location and set velocities of the components when I’ve done it in blueprint it’s fine because the array returned is of type UStaticMeshComponent not UActorComponent. This is what I have currently and the issue it within the for loop when I’m trying to receive the location for the component.

TArray<UActorComponent*> StaticMeshComps;
 StaticMeshComps = actorHit->GetComponentsByClass(UStaticMeshComponent::StaticClass());
 
 for (int y = 0; y < StaticMeshComps.Num(); y++)
 {
     UE_LOG(LogTemp, Warning, TEXT("Static Mesh: %s"), *StaticMeshComps[y]->GetName());
 
     FVector MeshLocation = StaticMeshComps[y]->GetComponentLocation();
 }

is there any way to change the code i have to make it work or anything else?

1 Like

I’ve managed to figure this one out but ill post the solution anyway as it took me a while to get what I needed.

So once I had my array of UActorComponents I was trying to cast the whole Array over to UStaticMeshComponet but I was having difficulties. In the end, I passed the array into the for loop like before and then once dealing with individual components I was able to cast the component type over.

This is what I ended up with and it works perfectly for me.

TArray<UActorComponent*> StaticMeshComps;
StaticMeshComps = actorHit->GetComponentsByClass(UStaticMeshComponent::StaticClass());

for (int y = 0; y < StaticMeshComps.Num(); y++)
{
	UStaticMeshComponent* MeshComp = Cast<UStaticMeshComponent>(StaticMeshComps[y]);

	FVector MeshLocation = MeshComp->GetComponentLocation();

       //Rest of Code
}
2 Likes

Thank you so much! You helped me a lot.

hleped me after six years!!! thannnnnnnnnnnnnnnnnnk u