Initialising a UStaticMeshComponent with a UPROPERTY UStaticMesh; mesh does not get set and is invisible

Hello,

I am just looking into the C++ side of UE4 so forgive me if this is a basic question.

I have an actor with a UStaticMeshComponent as the RootComponent, with a UStaticMesh as a UPROPERTY. I want be able to create a blueprint that inherits from this and specifies the particular mesh. The problem is that the mesh always seems to be invisible and does not get set correctly if I do set the static mesh to this specified value in the constructor. My assumption is that defaults do not become valid until some later stage but I cannot figure out the correct one.

BlockCpp.h

UCLASS()
class UNREALFORTZ_API ABlockCpp : public AActor
{
	GENERATED_BODY()

	UStaticMeshComponent* BlockMesh;

public:	
	UPROPERTY(EditAnywhere)
	UStaticMesh* SpecificBlockMesh;

	ABlockCpp();
};

BlockCpp.c

ABlockCpp::ABlockCpp()
{
	PrimaryActorTick.bCanEverTick = true;

	BlockMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BlockMesh"));
	RootComponent = BlockMesh;

	// This is fine.
	//
	// static ConstructorHelpers::FObjectFinder<UStaticMesh> BlockMeshAsset(TEXT("/Engine/BasicShapes/Cube"));
	// BlockMesh->SetStaticMesh(SpecificBlockMesh);

	BlockMesh->SetStaticMesh(SpecificBlockMesh);
}

If I print a debug message checking if SpecificBlockMesh == NULL, the condition is True so there must be some point where it gets set correctly.

I have managed to get it to show up when spawning, but it still doesn’t show up in the editor for some reason.

Any help would be appreciated!

Hello daniel.simulspace,

I have a suggestion. Try setting the BlockMesh component to a UPROPERTY(BlueprintReadWrite, EditAnywhere) and then you can set the static mesh in the viewport if you create a blueprint for the class. If you try to do it this way, you will not need the SpecificBlockMesh.

Have a great day,

Sean Flint

Hello daniel.simulspace,

I am marking this thread as resolved as I have not heard from you in a few days. If there are any further questions regarding this topic, feel free to reply to this thread. Otherwise, please post any new questions in a new AnswerHub topic.

Have a great day,

Sean Flint

Hi Sean,

Apologies yes — this helped! Thank you1

Cheers,
Daniel

Whoops, wrong account.

Hi! What if I don’t want to create a Blueprint class later? How can I make this work with a StaticMesh* property? Thanks.