GetPawn() in AIController always gives me null

Hi everybody! I have a problem with AIController class: when I call GetPawn() function it always give me a null pointer.
This is my AMyAIController.cpp:

void AMyAIController::setDestination(FVector destLocation) {
	targetLocation = destLocation;
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::, FString::Printf(TEXT("your click: X=%f, Y=%f, Z=%f"), targetLocation.X, targetLocation.Y, targetLocation.Z));

	APawn* pawn = GetPawn();

	if (pawn) {
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::, FString::Printf(TEXT("I've found my Pawn!")));
	}
}

And this is MainCharacter.cpp:

AMainCharacter::AMainCharacter() {
	AIControllerClass = AMyAIController::StaticClass();
}

what am I doing wrong?

Hello AleSpini,

I believe the function that you’re looking for is GetControlledPawn which is a function of AController. AAIController inherits from AController so you’ll be able to access this function. You can find more functions of these two classes here:

AController: AController | Unreal Engine Documentation

AAIController: AAIController | Unreal Engine Documentation

Hope this helps!

Thank you! I tried your solution, but it didn’t work as well. Then I checked through the AController functions and I found GetCharacter(). Using that function resolved my problem. Thank you!

Sorry, my last comment isn’t true, I changed some things in my code, then I used GetControlledPawn() and all worked! So the right function to call is GetControlledPawn().
Thank you .