Download

GetUserWidgetObject returns null

I have a widget component inside of my character:



// ABCharacter.h

...
UPROPERTY(VisibleAnywhere, Category = UI)
    class UWidgetComponent* HPBarWidget;
...




// ABCharacter.cpp
...

AABCharacter::AABCharacter()
{
     // Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

    HPBarWidget = CreateDefaultSubobject<UWidgetComponent>(TEXT("HPBARWIDGET"));
    HPBarWidget->SetupAttachment(GetMesh());
}


I checked HPBarWidget is null and it wasn’t:



if (HPBarWidget == nullptr) {
    UE_LOG(ArenaBattle, Error, TEXT("NULL"));
}


HOWEVER, When I try to get the widget object, it said it’s null.



if (HPBarWidget->GetUserWidgetObject() == nullptr) {
    UE_LOG(ArenaBattle, Error, TEXT("NULL"));      // it printed NULL
}


What causes this returns null? I can clearly see I have a WidgetComponent that exists in my character’s tree, and HPBarWidget wasn’t null too.

I struggled this already hours and couldn’t find any related issues. What did I wrong?

Using 4.24.3. Thanks.