I can't figure out why my cast returns nullptr

Hello. I have been trying to make actors clickable in c++ but for some reason my cast fails and I get a nullptr. I think I might be doing my cast wrong, can anybody tell me how to do it right? Here is the code:

void AMyCharacter::LeftClickDown()
{
	AMyPlayerController* PC = Cast<AMyPlayerController>(GetWorld()->GetFirstPlayerController());

	bool PlayerIDToBool;
	if (PC->PlayerID == 0)
		PlayerIDToBool = false;
	else
		PlayerIDToBool = true;

	if ( PlayerIDToBool == PC->Turn)
	{
		FHitResult Hit;
		GetWorld()->GetFirstPlayerController()->GetHitResultUnderCursor(ECC_Pawn, false, Hit);
		if (Hit.GetActor() != nullptr)
		{
			APiecesParent* HitPiece = Cast<APiecesParent>(Hit.GetActor());
			if (HitPiece != nullptr) 
			{
				GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("This pointer is not null")));
				if (PlayerIDToBool != HitPiece->Side)
				{
					if (SelectedActor != nullptr)
					{
						SelectedActor->Billboard->bVisible = false;
						SelectedActor = HitPiece;
						SelectedActor->Billboard->bVisible = true;
					}
					else
					{
						SelectedActor = HitPiece;
						SelectedActor->Billboard->bVisible = true;
					}
				}
			}
		}
	}
}

Thank you for taking your time.

EDIT: Here is the crash message:

LoginId:bea22a844573103f7d5d259a409dc4e4
EpicAccountId:4d48fb5a01db4c9992a73c08f9e4028e

Access violation - code c0000005 (first/second chance not available)

UE4Editor_Cheessy_Win64_DebugGame!AMyCharacter::LeftClickDown() [c:\users\silly\onedrive\documents\unreal projects\cheessy\source\cheessy\mycharacter.cpp:129]
UE4Editor_Cheessy_Win64_DebugGame!TBaseUObjectMethodDelegateInstance<0,AMyCharacter,TTypeWrapper __cdecl(void)>::Execute() [c:\program files (x86)\epic games\4.15\engine\source\runtime\core\public\delegates\delegateinstancesimpl.h:767]
UE4Editor_Engine!FInputActionUnifiedDelegate::Execute() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\engine\classes\components\inputcomponent.h:196]
UE4Editor_Engine!UPlayerInput::ProcessInputStack() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\engine\private\userinterface\playerinput.cpp:1243]
UE4Editor_Engine!APlayerController::ProcessPlayerInput() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\engine\private\playercontroller.cpp:2376]
UE4Editor_Engine!APlayerController::TickPlayerInput() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\engine\private\playercontroller.cpp:4051]
UE4Editor_Engine!APlayerController::PlayerTick() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\engine\private\playercontroller.cpp:2039]
UE4Editor_Engine!APlayerController::TickActor() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\engine\private\playercontroller.cpp:4133]
UE4Editor_Engine!FActorTickFunction::ExecuteTick() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\engine\private\actor.cpp:125]
UE4Editor_Engine!FTickFunctionTask::DoTask() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\engine\private\ticktaskmanager.cpp:269]
UE4Editor_Engine!TGraphTask::ExecuteTask() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\core\public\async\taskgraphinterfaces.h:883]
UE4Editor_Core!FNamedTaskThread::ProcessTasksNamedThread() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\core\private\async\taskgraph.cpp:954]
UE4Editor_Core!FNamedTaskThread::ProcessTasksUntilQuit() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\core\private\async\taskgraph.cpp:701]
UE4Editor_Core!FTaskGraphImplementation::WaitUntilTasksComplete() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\core\private\async\taskgraph.cpp:1798]
UE4Editor_Engine!FTickTaskSequencer::ReleaseTickGroup() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\engine\private\ticktaskmanager.cpp:538]
UE4Editor_Engine!FTickTaskManager::RunTickGroup() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\engine\private\ticktaskmanager.cpp:1450]
UE4Editor_Engine!UWorld::RunTickGroup() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\engine\private\leveltick.cpp:758]
UE4Editor_Engine!UWorld::Tick() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\engine\private\leveltick.cpp:1373]
UE4Editor_UnrealEd!UEditorEngine::Tick() [d:\build++ue4+release-4.15+compile\sync\engine\source\editor\unrealed\private\editorengine.cpp:1630]
UE4Editor_UnrealEd!UUnrealEdEngine::Tick() [d:\build++ue4+release-4.15+compile\sync\engine\source\editor\unrealed\private\unrealedengine.cpp:391]
UE4Editor!FEngineLoop::Tick() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\launch\private\launchengineloop.cpp:3025]
UE4Editor!GuardedMain() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\launch\private\launch.cpp:166]
UE4Editor!GuardedMainWrapper() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:134]
UE4Editor!WinMain() [d:\build++ue4+release-4.15+compile\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:210]
UE4Editor!__scrt_common_main_seh() [f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:264]
kernel32
ntdll

Cast looks ok to me. That means either PlayerController is not AMyPlayerController or there no PlayerController that exists, double check your default classes configuration in Project Settings as well as World Setting which can override the project settings

I have checked them and they are all fine. Also it is not AMyPlayerController because I can access the variables from it, also from my tests, it is getting stuck at if(HitPiece != nullptr) :frowning:

Thank you for your answer.

that means actor is wrong type, or GetHitResultUnderCursor fails

I figured that out yesterday. I changed it to GetHitResultUnderCursorForObjects and it no longer fails…but it crashes weird now. For example if I press on a white piece (side is false, PlayerIDToBool also false) it crashes straight away. But if I press on a black piece it does the cast and does nothing (as it should) and then I no longer crash on the white piece press. So I think my null pointer might be from the player controller afterall.

P.S.: I tried to make the same thing in blueprints and it works just fine.

EDIT: Is there anyway it might be related to the replication? This is a client based event (not related to the server in anyway) and in 1 player mode, it has no effect. But in 2 players mode I encounter the crash.

I changed my code a little bit and I fixed my issues. The crash still happens but only in PIE and not in Standalone Game or a packaged project. This looks like an engine bug to me so I will report it and hope it will get fixed :slight_smile:

Maybe a cast to APlayerController then check for IsLocalController() first before doing another cast to AMyPlayerController. It works for me.