Widget instance is null

MyWidgetInstance = CreateWidget(GetWorld(), WidgetClass);

MyWidgetInstance is null
UMyUserWidget is my actual widget name

Did you set WidgetClass either in the blueprint or via ConstructionHelpers?

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

#include “Components/TextBlock.h”
#include “MyUserWidget.h”

void UMyUserWidget::NativeConstruct()
{
Super::NativeConstruct();

// ItemTitle can be nullptr if we haven't created it in the
// Blueprint subclass
if (ItemTitle)
{
	ItemTitle->SetText(FText::FromString(TEXT("Hello world!")));
}

}

void UMyUserWidget::Update()
{

// ItemTitle can be nullptr if we haven't created it in the
// Blueprint subclass

	ItemTitle->SetText(FText::FromString(TEXT("Hello!")));

}

This part works and display hello world correctly on the screen.

But where are you setting WidgetClass?

It’s a variable the I have declared in the actor class.

UPROPERTY(EditAnywhere, Category = "Widgets")
	TSubclassOf<UMyUserWidget> WidgetClass;

Where should I set it and how?

Create a Blueprint instance of your Actor, and you’ll see that Widget Class will be available as a property of the blueprint that you can modify however you wish.

Learn Unreal Engine (with C++) - Full Course for Beginners - YouTube - I guess that his what you mean?

Yes, exactly.

Thanks! From time to time I use ChatGPT, but it gets things really wrong. Is it possible to set this in code or is it better to use the video approach?

From what I’m aware of, no. The best way to get the widget class is through creating the blueprint instance and setting it there.