I don't know why this code make crash

void ALopolProjectPlayerController::TotalControlFunction(const ETouchIndex::Type FingerIndex, const FVector Location)
{

GetHitResultUnderFinger(ETouchIndex::Touch1, ECC_Camera, true, TouchedResult);

ALopolProjectCharacter* TouchedActor = Cast<ALopolProjectCharacter>(TouchedResult.Actor);

if (TouchedActor->IsEnemy() == false)
{
	ALopolPlayerCharacter* TouchedPlayerCharacter = Cast<ALopolPlayerCharacter>(TouchedActor);

	if (TouchedPlayerCharacter)
	{

		TouchedPlayerCharacter->SetIsCharacterSelected(true);
		bCheckCharacterSelected = TouchedPlayerCharacter->IsCharacterSelected();

		TouchCheckDisplay = UKismetStringLibrary::Conv_BoolToString(bCheckCharacterSelected);
		GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Red, TouchCheckDisplay);

		
	}

}

It always makes the crash wherever I click, except clicking the ‘LopolPlayerCharacter’. I don’t know why this occur.
Where is incorrect?

Below is Crash Information.

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

UE4Editor_LopolProject_7090!ALopolProjectCharacter::IsEnemy() [f:\lopolproject\source\lopolproject\lopolprojectcharacter.cpp:162]
UE4Editor_LopolProject_7090!ALopolProjectPlayerController::TotalControlFunction() [f:\lopolproject\source\lopolproject\lopolprojectplayercontroller.cpp:133]
UE4Editor_LopolProject_7090!TBaseUObjectMethodDelegateInstance<0,ALopolProjectPlayerController,TTypeWrapper __cdecl(enum ETouchIndex::Type,FVector)>::Execute() [c:\program files\epic games\ue_4.21\engine\source\runtime\core\public\delegates\delegateinstancesimpl.h:618]
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Core
UE4Editor_Core
UE4Editor_Core
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
kernel32
ntdll

Edit line 5 from this

if (TouchedActor->IsEnemy() == false)

to this

if (TouchedActor && TouchedActor->IsEnemy() == false)

It should prevent the crash as it will skip everything if the Cast fails.

I would try the same thing. It sounds to me like that the pointer to “TouchedActor” returns NULL, which then results in a crash (unreal does not like NULL pointers). One way is to circumnavigate this problem by doing what is mentioned above my response. I would also try to figure out why the cast fails in the first place.

Very thank you for some informations about it… It would be very helpful to me. Thank you.