Why does the pawn from my controller == NULL?

So I setup my pawn to be autopossessed by an AI controller class using this inside of my pawn class:

AIControllerClass = AAIControllerUnit::StaticClass();
AutoPossessAI = EAutoPossessAI::PlacedInWorldOrSpawned;

However, for some reason the controller that gets spawned has the pawn set to NULL. ie. when I call GetPawn() it returns null. I checked to make sure that the controller is getting possessed by it and the function PossessedBy() does in fact fire, but upon closer inspection even there the controller has pawn equal to null. Its weird because the pawn recognizes the controller but the controller doesn’t know the pawn exists. Any help would be appreciated!

Edit: I tried putting it in the comments but it didn’t format right so here is how I spawned my actor:

this->spawnedUnit = (AUnitPawn*)world->SpawnActor(BPUnitType, &spawnLocation, &spawnRotation, spawnParams);
this->spawnedUnit->team = this->team;
this->spawnedUnit->SpawnDefaultController();

It’s definitely not possessing the pawn though I don’t know why.
Apparently its also happening on other spawned units in my game as well not just this one.

So you are calling the “Possess” function and its Super::Possess? Do you spawn or place the AI Pawn?

void APawn::SpawnDefaultController()
{
if ( Controller != NULL || GetNetMode() == NM_Client)
{
return;
}
if ( AIControllerClass != NULL )
{
FActorSpawnParameters SpawnInfo;
SpawnInfo.Instigator = Instigator;
SpawnInfo.bNoCollisionFail = true;
SpawnInfo.OverrideLevel = GetLevel();
AController* NewController = ()->SpawnActor(AIControllerClass, GetActorLocation(), GetActorRotation(), SpawnInfo);
if (NewController != NULL)
{
// if successful will result in setting this->Controller
// as part of possession mechanics
NewController->Possess(this);
}
}
}

Looking on code there 4 cases when pawn it’s not possesed by that function

  1. Pawn is already possessed
  2. You trying to do it on client-side (Only server can have PlayerControllers in memory)
  3. Default AI controller is not set
  4. Spawn of that controller failed

Look like 4 is most likely the possibility if thats the case reason of failed spawn should be in log ( you can find it in Saved/Log). Or else you making multiplayer game then 2 is also possibility

Not exactly this is how I spawned my actor (though I believe its supposed to call Possess()
this->spawnedUnit = (AUnitPawn*)world->SpawnActor(BPUnitType, &spawnLocation, &spawnRotation, spawnParams);
this->spawnedUnit->team = this->team;
this->spawnedUnit->SpawnDefaultController();

I looked through the log and I couldn’t find anything that mentioned the controller. I think that the controller is in fact spawning though because I can access it. The controller doesn’t return null only the getpawn() function. Also idk if its relevant but when I put in a breakpoint in the function PossessedBy(AController* NewController) in my pawn, Paw, Character, and PlayerState all are null inside of the controller.

So I did some more digging and stepped through the code and I found out where its going wrong. So when NewController->Possess(this); is called it goes through some more checks and eventually end up at OnPossess(InPawn) from here there is a bunch of stuff that I can’t follow as a lot of it simply says file not found. However eventually it ends up in void AController::Possess(APawn* InPawn) This is where the problem is because somewhere between the Possess from AAIController and here the pawn variable disapears. When I looked at the varialbes for “this” pawn could be found in AAIController::Possess but not AController::Possess. Hope this can maybe help figure it out some.

So I finally “fixed” the issue. I still have no clue why it wasn’t working but I created a new class derived from Pawn and then copied the code over from the old one and now it works fine. Not sure what the issue was with the old one (it’s literally the same exact code I basically copy and pasted) but at least its working now.