Why does Unreal Engine crash when I try to do a switch statement?

I’ve been trying to do a raycast and seeing what physical material it hits and doing damage based on the physical material hit on a player but every time I hit the player Unreal Engine crashes.
Any ideas on how I could implement this without it crashing?

Player.CPP

//Health is the UHealthComponent and HP is the health value of the component on the player
//Target is the player hit reference and Hit is the FHitResult hit info
switch (Hit.PhysMaterial.Get()->SurfaceType)
		{
		case 1:
			UGameplayStatics::ApplyPointDamage(Target, 10.0f, Hit.TraceStart, Hit, GetInstigatorController(), this, UDamageType::StaticClass());
			GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, FString::SanitizeFloat(Target->Health->HP));
			break;
		case 2:
			UGameplayStatics::ApplyPointDamage(Target, 20.0f, Hit.TraceStart, Hit, GetInstigatorController(), this, UDamageType::StaticClass());
			GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, FString::SanitizeFloat(Target->Health->HP));
			break;
		default:
			UGameplayStatics::ApplyPointDamage(Target, 30.0f, Hit.TraceStart, Hit, GetInstigatorController(), this, UDamageType::StaticClass());
			GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, FString::SanitizeFloat(Target->Health->HP));
		}

Thanks in advance.

Most likely the PhysMaterial is NULL and you’re dereferencing it which causes crash (unlike BP where it just logs an error message).

BTW your code should be shortened to the form:

auto Amount = FMath::Clamp(Hit.PhysMaterial.Get()->SurfaceType * 10.0f, 10.0f, 30.0f);
UGameplayStatics::ApplyPointDamage(Target, Amount, Hit.TraceStart, Hit, GetInstigatorController(), this, UDamageType::StaticClass());
GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, FString::SanitizeFloat(Target->Health->HP);

Thanks for the recommendation I’ll implement it now.

Thanks I also think that’s the problem but how do I access the PhysMaterial?(It’s a custom PhysMaterial)