Editor Crashes on opening Blueprint

Making an RTS / Tycoon game, I’ve got to the stage of an AI character going to an actor which meets a specific need (food, drink, toilet, etc.)

When I create a new blueprint sub-class from the main C++ class of interactive actors, everything works fine. I can set the mesh, the need being met and so on.

The problem is that every time I try to open one of these blueprints, the editor crashes with an unhandled exception.

Here’s the code based off the standard AddDynamic OnComponentBeginOverlap event seen in most tutorials.

	if (OtherActor)
	{
		auto ControlledPawn = OtherActor->GetInstigator();
		AMyAIController* PawnController = Cast<AMyAIController>(ControlledPawn->GetController());
		
		if (PawnController)
		{
			UE_LOG(LogTemp, Warning, TEXT("Cast Successful"));
		}
		else
		{
			UE_LOG(LogTemp, Warning, TEXT("No MyAIController Class"));
		}
	}

According to the error logs, the issue is the line where I cast to the AMyAIController but this works fine if I keep the blueprint open, only when I close it and open it afterwards.

If I comment out the text from that line and recompile, the blueprint opens with no problems. The #include is also there for the AI Controller.

I presume the issue is a nullptr but it doesn’t happen when i first create the blueprint so what am I missing here?

I solved this one. The Controlled Pawn has to be cast to APawn or it results in a nullptr.

Should read:

APawn* ControlledPawn = Cast<APawn>(OtherActor->GetInstigator());