GetStaticMeshComponent() / StaticMeshActor cast not working but FindComponentByClass<UStaticMeshComponent>() working

I was trying to change the material of a static mesh, however casting to a StaticMeshActor and using GetStaticMeshComponent was not working. My program has a PointSpawner class which spawns points. I have a MyPoint blueprint actor which has a static mesh and the PointSpawner class spawns instances of that point. However when I try to cast the spawned point to a Static Mesh Actor, Unreal crashes. Why does the cast to StaticMeshActor fail even though the BP Actor has a static mesh component? Why does FindComponentByClass work when the GetStaticMeshComponent fails? Here are some more pictures for information:
What works:


What doesn’t:

MyPoint BP Actor:

Hi!

As far as I understand, your spawned actor has class AActor, not AStaticMeshActor. Having static mesh component doesn’t make it AStaticMeshActor.

I guess that makes sense, but I don’t understand why the cast to AStaticMeshActor fails.

Because MyPoint is a child of class Actor instead of StaticMeshActor, as I can see in your screenshot:

If you reparent it to StaticMeshActor, then it probably won’t crash.

Okay yes that worked, thank you.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.