Static Mesh not appearing in blueprint derived from custom class

Greetings,

I’m having a little issue that has been plaguing me for a while now. I’m adding some custom logic to a certain Actor in order to extend it and create a “Pickup” class (that will, by default, manage all kind of animations, shine and whatnot).

My class has a defined UStaticMeshComponent in order to handle this:

UPROPERTY(VisibleDefaultsOnly, Category = Logic) 
UStaticMeshComponent* PickupMesh;

Then, in Pickup's constructor:

FObjectInitializer* init = new FObjectInitializer();
PickupMesh = 
    init->CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("PickupMesh"));
PickupMesh->SetSimulatePhysics(false);
PickupMesh->AttachTo(RootComponent);

Here’s the thing: When I create a blueprint choosing my GE_Pickup class as Parent class and assign a mesh to PickupMesh it appears for a fraction of a seccond and then disappears. It’s not rendered, though it remains assigned. The really curious part about this is that if I duplicate PickupMesh the new one works correctly, appearing and floating as expected.

Any idea as to what am I missing?

You are not using standard ObjectInitializer setup, do not create it yourself

Instead use a proper UE4 C++ constructor.

I appreciate your inventiveness but this is also the source of your extended time on this issue.

AJoyProjectile::AJoyProjectile(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{

	//~~~~~~~~~~~~~~~~~~
	CollisionComp = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("SphereComp"));
	
	//Set Root
	RootComponent = CollisionComp;
	//~~~~~~~~~~~~~~~~~

In Your .h in a public: section

AJoyProjectile(const FObjectInitializer& ObjectInitializer);

Enjoy!

Rama

Thanks for your response, Rama! What strikes me as curious is that, by default, when creating a class this ObjectInitializer is not included in the C++ code template, at least in 4.7. Previous vesrions include it, but not anymore. I thought This would be an intended approach when it worked in other scenarios but, specifically in this one, it didn’t come through. Worked like a charm!, thank you!