CreateWidget cannot be used on Player Controller with no attached player

I’m calling UGameplayStatics::CreatePlayer to create a second local player. I can confirm from the World Outliner that a second player controller is created, together with all supporting classes (PlayerState, Pawn, etc.).

At the Controller BeginPlay, in the Blueprint, I’m calling CreateUserWidget to create the UI for the player. At that point I get an error: “CreateWidget cannot be used on Player Controller with no attached player”.

Shouldn’t this be done by default? I’ve, alternatively, called CreatePlayer from the GameMode blueprint, but the result is the same.

I got an excellent answer in another place, in sum:

No, you can’t guarantee that the player has been set in BeginPlay(). I haven’t followed the actor spawn chain in a while but I believe it in this case goes something like this:

UGameplayStatics::CreatePlayer() calls UGameInstance::CreateLocalPlayer() which creates the ULocalPlayer. UGameInstance::CreateLocalPlayer() then calls ULocalPlayer::SpawnPlayActor() which then calls UWorld::SpawnPlayActor() which calls AGameMode::Login() which creates the Player Controller by calling UWorld::SpawnActor() which calls AActor::PostSpawnInitialize() which calls AActor::FinishSpawning() which calls AActor::PostActorContruction() which calls AActor::BeginPlay().

Only when it returns to UWorld::SpawnPlayActor() does the Player get set. In your case here since you’re in Blueprints. Just create a function on your player controller that creates the widget, and call it after you call UGameplayStatics::CreatePlayer()., That way you are sure everything is hooked up right.

I move the code to create the widget to a HUD class and everything worked fine.