How to expose a UStaticMeshComponent to be assigned in Blueprints

I have written an actor class in C++.

I have a blueprint that inherits from this actor class and contains multiple static meshes.

I would like to reference a particular one of these static meshes from within blueprints.

I tried doing this by adding the following to the actor’s header file

UPROPERTY(EditAnywhere)
UStaticMeshComponent* SurfaceMesh = nullptr;

but this did not cause anything to show up in the editor.

Is there any way to do this? If not, what is my best alternative?

try this.

class UStaticMeshComponent* SurfaceMesh;

Try to add

UPROPERTY(EditAnywhere, BlueprintReadWrite)

That doesn’t work either unfortunately

No luck with that either.

UPROPERTY(EditAnywhere, BlueprintReadWrite)
class UStaticMeshComponent* SurfaceMesh;

Hey there, try:

UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = "Test")

And don’t forget to initialize it on the constructor:

Mesh = ObjectInitializer.CreateDefaultSubobject(this, TEXT(“Mesh”));

Hi thanks for the reply. I am not trying to initialize a new mesh, I have a preexisting mesh within the blueprint that I want to have access to in code.

I have worked around the problem by calling GetComponentsByClass and then looping through the results to find the one I require, but this seems inefficient. I would much prefer if the reference was serialized out to the editor.

And how do you create the mesh? using NewObject? If you create it somewhere you can also store the reference to it in code.

It is not being created at runtime. The blueprint class contains the mesh as a component.

So if you add the static mesh in blueprints and you’re trying to access it by c++ then the only way to get that is to go through all of the static mesh components using GetComponentsByClass and checking the name. The thing is you can do that in BeginPlay and it will only happen once, so it’s fine.

Ok, I’ll keep doing it that way. That’s working for me. Thanks for your help.

Eventually you could use GetcomponentByTag and you would just tag the component you want, but that should iterate through them, so it should be same thing.

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “BodyPart”)

UStaticMesh* BodyPart;