CreateDefaultSubobject stopped working

When I try to add components to new classes using CreateDefaultSubobject no components are to be found in the blueprint afterwards. I have even tried doing this in new projects and it is reproducible from a fresh project. I thought it could have been a bug in 4.26.1 but after installing 4.25.4 as a potential quick fix to get me writing code again I realised that it quickly started happening there too.

In my BugTest project this is my header file for my pawn, written EXACTLY how I have always done so with no issue.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "BTPawnBase.generated.h"

class UCameraComponent;

UCLASS()
class BUGTEST_API ABTPawnBase : public APawn
{
	GENERATED_BODY()

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
	UCameraComponent* CameraComp;

public:
	// Sets default values for this pawn's properties
	ABTPawnBase();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

};

And this is the code in its CPP file.

#include "Pawns/BTPawnBase.h"

#include "Camera/CameraComponent.h"
// Sets default values
ABTPawnBase::ABTPawnBase()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
	CameraComp->SetupAttachment(RootComponent);

}

// Called when the game starts or when spawned
void ABTPawnBase::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void ABTPawnBase::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void ABTPawnBase::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

}

While compiling I had UE4 closed and ran a developmental build. After a successful compile I opened the project and created a Blueprint class based on my PawnBase only to find that it had no camera component.

An apparent fix to this that I have found is to re-class the blueprint from the CPP parent to just “Pawn” or “Actor” depending on what class you are having issues with (my main project has this happening for some Actors but not all, but all pawns are pretty useless after writing it in code) but nothing I do fixes this issue.

This is a screenshot of the Blueprint parented to the above code.

After installing 4.25.4 I was able to add a camera component to my PawnBase but when following the same code I was not able to add ANY components to an actor class in the same project.

This issue has made me very confused and has completely halted work on my project, I have no idea why it started or how to fix it. Surely I am doing something wrong, I don’t see how such a major piece of code would just break like that, but at the same time I have had NO issues with CreateDefaultSubobject apart from spelling mistakes when I was first learning code. This especially feels buggy since it was working then just decided not to…

I should also mention that I tried verifying the UE 4.26 install but that also did not fix the issue. I haven’t tried a complete reinstall since I have the same issue in 4.25.4

Any help would be greatly appreciated!

Hello! Several times I have faced something like that, however recreating BP always was the cure… Sometimes C++ code just dont picked up to library after compilation. You can try to clean up project (remove .vs, Binaries, DerivedDataCache, Intermediate, Saved folders) and then compile this again.

This got my blueprints working correctly! Thank you so much! I have no clue why its persistent across projects but I now know how to fix it! Thanks again!

I don’t know how to make this the answer, I’m new to this :stuck_out_tongue:

Just fill the field after “Answers” label and click on Post your answer

The problem for me was that I accidentally started using the wrong build task when compiling my code. I don’t know when or why but I have been using “MyProject” build tasks and that isn’t compiling my project correctly, however, “MyProjectEditor” build tasks work as expected.

Thanks Kehel18 for the recommendation to manually clean up my project files, this gave me a lot of insight into why this was happening and I was able to figure it out from there.