Download

Set Static Mesh in C++

I wont to Set my Mesh with C++ cood. The problem is, it don’t work. I’ve already tried, to set the mesh, in the constructor, “BeginPlay”, and in the Tick function.



static FConstructorStatics ConstructorStatics;

DummyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("Dummy0"));
RootComponent = DummyRoot;

GhostMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("GhostMesh0"));
GhostMesh->SetupAttachment(DummyRoot);
GhostMesh->SetStaticMesh(ConstructorStatics.TestBuildin_Finel.Get());


If I create a Blueprint from my C++ Class, and try to set it in the Bluprints, it work.
BP.PNG

I have no idea, why is don’t work, in C++.

You’re not using Constructor Statics correctly, you need to actually find the object by it’s string asset reference, tbh I’m not sure how that even compiles.



	static ConstructorHelpers::FObjectFinder<UStaticMesh>MeshAsset(TEXT("StaticMesh'/Game/MyMesh.MyMesh"));
	UStaticMesh* Asset = MeshAsset.Object;

        GhostMesh->SetStaticMesh(Asset);


You can ‘get’ the string reference of an asset by right-clicking it and hitting “Copy Reference”.

However, if that mesh never changes during gameplay, then why not just set it directly on the mesh component when you create a blueprint of that class. Other than for specific cases it’s not a good workflow to reference assets this way.

Thank you, @TheJamsh !!!

Or you can use the way like this Change StaticMesh runtime via C++ - Unreal Engine Forums