Widget via C++ with UWidgetComponent

I cant add a UWidgetComponent to my Actor via C++ because it always throws errors at me I tried doing it the following way:

.H

	UPROPERTY(EditAnywhere)
	class UWidgetComponent* WidgetComp;

.CPP

AGatherableActor::AGatherableActor(const class FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
 	// 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;

	WidgetComp = ObjectInitializer.CreateDefaultSubobject<UWidgetComponent>(TEXT("Widget"));
}

You did do both those things? What errors do you get exactly?

I’m afraid that is not the issue here

EDIT:
I added the UMG module to the Build.cs and changed my Constructor line of code to this:

WidgetComp = ObjectInitializer.CreateDefaultSubobject<UWidgetComponent>(this, TEXT("Widget"));

now it works fine

It is fixed now thanks to you mostly

Did you include the header file?
Also, you might want to add the UMG module to your Build.cs file.

Nice! Could you mark my answer as correct maybe? :slight_smile: