C++ created component is not added to Blueprint

I created a pawn class.
Created a Bluprint from it.
Added the lines

private:

	class UCapsuleComponent* carCapsule;

in the .h file.
Added the

#include "Components/CapsuleComponent.h"

in the .cpp file and then

carCapsule = CreateDefaultSubobject<UCapsuleComponent>( TEXT( "Car Capsule" ) );

Everything compiled perfectly. No errors.
BUT the capsule component does NOT show up in the Blueprint. I did the same process three times and even restarted Unreal.
Nothing shows up in the Blueprint.
Any help would be much appreciated.

1 Like

o/
Any UObject* must have UPROPERTY() macro otherwise there is risk of it being GC’ed
You should also setup attachment or set it as a root. Check SetupAttachment()
After these try again. If won’t help poke me here

2 Likes

Your advice is noted with respect.
I deleted the Binaries, Derived Data Cache, Intermediate and Saved folder, rebuilt the Vs project and launched and all ok.
Thanks for the advice.

1 Like
UPROPERTY(EditDefaultsOnly)
class UCapsuleComponent* CarCapsule;

or if you are using UE5 the raw pointers are now supposed to be wrapped

UPROPERTY(EditDefaultsOnly)
TObjectPtr<class UCapsuleComponent> CarCapsule;

The “EditDefaultsOnly” vs “EditAnywhere” depends on the usage of the component. One allows you to edit only in BP editor, other allows to edit even when the object is instanced in the level

1 Like