Instanced Widget Component not shown in the Viewport

Hi everyone.

I have been using code to create a Widget Component to show a Confirm or cancel option in the HUD, and I have set it to the correct Class and the Widget Space to “Screen” value, but I can’t seem to be able to display the HUD in the World even though when I check the Actor the Component it’s attached correctly and it’s using the corresponding Class, and after the component is added if I modify in the Editor anything related to the widget component, it starts to paint it correctly in the viewport, I will post the code down bellow and I hope that someone here can help me out with this issue.


void ABuilding::SetupBuildingWidget()
	UWidgetComponent* Widget = NewObject<UWidgetComponent>(this, UWidgetComponent::StaticClass(), TEXT("Building Widget"));

	if (!Widget)

	FStringClassReference BuildConfirmReference(TEXT("/Game/UI/BuildBuildingHUD.BuildBuildingHUD_C"));

	if (UClass* ConfirmClass = BuildConfirmReference.TryLoadClass<UUserWidget>())

		UUserWidget* ConfirmBuildWidget = CreateWidget<UUserWidget>(GetWorld(), ConfirmClass);

		Widget->bHiddenInGame = false;
		Widget->bVisible = true;
		Widget->AttachToComponent(RootComponent, FAttachmentTransformRules::SnapToTargetIncludingScale);


and here is a Screenshot of the Widget has it should appear


And here is what is happening:

Any help with this would be greatly appreciated and another thing is that If I edit any value from the widget from within the PIE, it automatically renders the Widget correctly in the screen. Thank you.