Cannot create widget using c++

Hi all,

i’m actually using this code to create a Widget and add it to viewport, but fails on createwidget function.

UHUDController::UHUDController() {
	ConstructorHelpers::FClassFinder<UMainMenuWidget> WidgetAsset(TEXT("WidgetBlueprint'/Game/HUD/MainMenu/MainMenu.MainMenu_C'"));
	if (WidgetAsset.Succeeded())
	{
		WidgetClass = WidgetAsset.Class;
	}
	if (WidgetClass)
	{
		MainMenuWidget = CreateWidget<UMainMenuWidget>(GetWorld(), WidgetClass);
		if (MainMenuWidget)
		{
			MainMenuWidget->AddToViewport();

		}
		else {
			GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Yellow, TEXT("125125125"));
		}
	}
}

The engine prints the debug message, so taht means that MainMenuWidget is actually null. Any reason for that? WidgetClass isn’t null since the code inside the if works. Any idea?

Okay,

As per the docs, you are trying to set world as the owner(First parameter of CreateWidget).

That won’t work. Try getting Character/Pawn classes and use them instead.
Like this,

This should work.