CreateDefaultSubobject crashes Editor if used with UWidgetComponent!

Hello,

I’m wanting to automatically add a WidgetComponent to an actor when I create a Blueprint of it. To do so, I’m trying to use “CreateDefaultSubobject” with UWidgetComponent.
My goal is to create quick InteractableObjects where I can just create a Blueprint version of the C++ file and it will come included with the WidgetComponent and make it of a certain class.

The problem is, now when I try to open the editor it crashes giving this error:

=====================================================================

Fatal error: [File:D:\build++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\Obj.cpp] [Line: 163]
Using incorrect object initializer.

0x00007fff1d33340b UnrealEditor-CoreUObject.dll!UnknownFunction
0x00000204f1c76be2 UnrealEditor-CapstoneProject.dll!AInteractableObject::AInteractableObject() [D:\Unreal Games\Rotating-Mini-World\Source\CapstoneProject\Private\InteractableObject.cpp:10]
0x00007fff1d0ff038 UnrealEditor-CoreUObject.dll!UnknownFunction
0x00007fff1d120383 UnrealEditor-CoreUObject.dll!UnknownFunction
0x00007fff1d5431e0 UnrealEditor-CoreUObject.dll!UnknownFunction
0x00007fff1d521497 UnrealEditor-CoreUObject.dll!UnknownFunction
0x00007fff1d3387d5 UnrealEditor-CoreUObject.dll!UnknownFunction
0x00007fff1de37fad UnrealEditor-Core.dll!UnknownFunction
0x00007fff1de5e300 UnrealEditor-Core.dll!UnknownFunction
0x00007fff2f2ea944 UnrealEditor-Projects.dll!UnknownFunction
0x00007fff2f2ead46 UnrealEditor-Projects.dll!UnknownFunction
0x00007ff7b1ed3459 UnrealEditor.exe!UnknownFunction
0x00007ff7b1ed7079 UnrealEditor.exe!UnknownFunction
0x00007ff7b1ece146 UnrealEditor.exe!UnknownFunction
0x00007ff7b1ece42a UnrealEditor.exe!UnknownFunction
0x00007ff7b1ed18a4 UnrealEditor.exe!UnknownFunction
0x00007ff7b1ee70c4 UnrealEditor.exe!UnknownFunction
0x00007ff7b1eea37a UnrealEditor.exe!UnknownFunction
0x00007ff81ecb7374 KERNEL32.DLL!UnknownFunction

=====================================================================

I already added the “UMG” module to the project.Build.cs so that’s not the issue here. I also rebuilt my project and deleted the Intermediate and Binary Files and Generated new VisualStudio Project files. I’m soo lost.

If I remove the line with CreateDefaultSubobject the editor opens.

Here’s my code so far:

InteractableObject.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Interfaces/Interactable.h"
#include "Components/WidgetComponent.h"
#include "InteractPromptWidget.h"
#include "InteractableObject.generated.h"

UCLASS()
class CAPSTONEPROJECT_API AInteractableObject : public AActor, public IInteractable
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AInteractableObject();

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

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

protected:

	UPROPERTY(EditAnywhere)
	class UWidgetComponent* HUD_Interact;

	
	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite)
	TSubclassOf<UInteractPromptWidget> InteractWidgetBlueprint;

};

InteractableObject.cpp

// Fill out your copyright notice in the Description page of Project Settings.
#include "InteractableObject.h"

// Sets default values
AInteractableObject::AInteractableObject()
{
 	// 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;

	HUD_Interact->CreateDefaultSubobject<UWidgetComponent>(TEXT("HUD_InteractionWidget"));
	//HUD_Interact->SetupAttachment(RootComponent);
	//HUD_Interact->SetWidgetClass(InteractWidgetBlueprint);

}

// Called when the game starts or when spawned
void AInteractableObject::BeginPlay()
{
	Super::BeginPlay();
	
	//HUD_Interact->InitWidget();
	
}

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

}


Project.Build.cs

// Fill out your copyright notice in the Description page of Project Settings.

using UnrealBuildTool;

public class CapstoneProject : ModuleRules
{
	public CapstoneProject(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
	
		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG" });

		PrivateDependencyModuleNames.AddRange(new string[] { });

		// Uncomment if you are using Slate UI
		// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
		
		// Uncomment if you are using online features
		// PrivateDependencyModuleNames.Add("OnlineSubsystem");

		// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
	}
}

Thank you for any help

Ok I’ve figured it out :slight_smile:

In the line where I declared the CreateDefaultSubobject I used → when I should use =.

Before (Doesn’t Work)

HUD_Interact->CreateDefaultSubobject(TEXT(“HUD_InteractionWidget”));

After (Works)

HUD_Interact = CreateDefaultSubobject(TEXT(“HUD_InteractionWidget”));