Download

Attach StaticMesh to Actor?

This should be simple but none of the tutorials on the Wiki did quite what I want…

I have a subclass of Actor, and I want to have a property that lets designers choose a StaticMesh to be displayed.


UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category = "Vehicle Part Properties")
UStaticMesh * StaticMeshAsset;

Also going to need a StaticMeshComponent…


UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Mesh")
UStaticMeshComponent * StaticMesh;

All I want to do is attach the StaticMesh to the Actor on creation.


AVehiclePart::AVehiclePart()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	// Attach a scene componet to root
	USceneComponent * SceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Root Component"));
	RootComponent = SceneComponent;

	StaticMesh->SetStaticMesh(StaticMeshAsset);

	// Attach the specified static mesh to the scene component
	StaticMesh->AttachTo(RootComponent);

}

This blows up UE4. Trying to adapt this tutorial. Thanks in advance.

You can use OnConstruction , and set your SM there



virtual void OnConstruction(const FTransform& Transform) override

AH perfect, thanks!