How do I get Actor's Static Mesh?

Hello. I am new into Unreal, that’s why I don’t know how to get Actor’s Static Mesh.

Here I have a RayCast() function, which tries to get object I am looking at, and it returns FHitResult type variable.

FHitResult AMyCharacterCPP::RayCast()
{
	FHitResult Hit = FHitResult();
	FVector Forward = CamComp->GetForwardVector();
	FVector Start = CamComp->GetComponentLocation();
	FVector End = (Forward * 80.0f) + Start;
	FCollisionQueryParams Col;
	Col.AddIgnoredActor(this);
	return Hit;
}

And here it is function, where I’m getting result from RayCast() and trying to know result’s Mesh Component(I call it every time I move my camera):

void AMyCharacterCPP::UpdateLookingPlace()
{
	obj = AMyCharacterCPP::RayCast().GetActor();
	// ObjMesh = ???
	/*if (std::find(ObjectsToUse.begin(), ObjectsToUse.end(), ObjMesh) != ObjectsToUse.end())
	{
		HUDMsgVal = 1;
	}
	else
	{
		HUDMsgVal = 0;
	} */
}

In part with if I’m just trying to know, does my list of meshes include ObjMesh. And that’s reason why also I can check NameID of this object, but when I try to use FString ObjName = obj->GetName(); and then to move the mouse, Unreal crashes and shows me this:


I tried everything I found in the internet, but I don’t know what to do.

Where’s the actual raytrace?

[...]
Col.AddIgnoredActor(this);
GetWorld()->LineTraceSingleByChannel(Hit, Start, End, ECC_Visibility, Col);
return Hit;

Next, why are you using AMyCharacterCPP::RayCast() as a static function? It’s in this very class, just call RayCast().

What is obj? If you need the hit mesh, just GetComponent() from hit result, not GetActor();

1 Like

This line:

GetWorld()->LineTraceSingleByChannel(Hit, Start, End, ECC_Visibility, Col);

The obj is result from RayCast(), but if I use GetComponent(), obj has to be UPrimitiveComponent * type. Maybe I don’t understand something, because I’m not pro, but I can’t find way how I can get Mesh name from UPrimitiveComponent *?

Yes. I added it, but I can’t see it in your code.

The obj is result from RayCast()

Is it AActor* ? Did you declare it in .h?

I’m sorry very much, I forgot to add this line here.

Yes, it is, and I declared it, but it doesn’t help, because GetComponent() returns UPrimitiveComponent *.

If you want to get a mesh from actor, you’ll have to cast to that actor class, and either have that component be public, of create a function in the actor class that will return the needed mesh.
As for UPrimitiveComponent, you can cast it to the class you need,

UStaticMeshComponent* StaticMesh = Cast<UStaticMeshComponent>(Hit->GetComponent());
if (IsValid(StaticMesh))
{
    // your code here
}

So, I tried your way, and added this lineGEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, MeshName.ToString());, which prints MeshName to screen, when I move the mouse, MeshName here is FName MeshName = Cast<UStaticMeshComponent>(RayCast().GetComponent())->GetStaticMesh()->GetBodySetupName();. But now it gives me only BodySetup message on every object I look at. Maybe you understood me incorrectly, because I need this name(for example):
Example

Hi @EgorMaksM !

I think what you want to do is instead of FName MeshName = Cast<UStaticMeshComponent>(RayCast().GetComponent())->GetStaticMesh()->GetBodySetupName(); is:

FName MeshName = Cast<UStaticMeshComponent>(RayCast().GetComponent())->GetStaticMesh()->GetName();

That should give you the name of the instance of the mesh you used, so in this case it would return something like Banker_Lamp_2_1 or something like that, then you could crop or get a substring of that to get the Static Mesh name

Finally, it worked! Thank you very much!