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

**URL:** <https://forums.unrealengine.com/t/why-does-unreal-engine-crash-when-i-try-to-do-a-switch-statement/596546>\
**Category:** C++\
**Tags:** Raycast, unreal-engine, UE4, question\
**Created:** [July 4, 2022, 7:00am UTC](https://forums.unrealengine.com/t/why-does-unreal-engine-crash-when-i-try-to-do-a-switch-statement/596546 "2022-07-04T07:00:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShadowSprite123](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ShadowSprite123](https://forums.unrealengine.com/u/ShadowSprite123)\
**Post date:** [July 4, 2022, 7:00am UTC](https://forums.unrealengine.com/t/why-does-unreal-engine-crash-when-i-try-to-do-a-switch-statement/596546/1 "2022-07-04T07:00:44Z")

</div>

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**

```auto
//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.

---

<div class="post-metadata">

**Author:** ![Chatouille](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chatouille/32/1194622_2.png) [@Chatouille](https://forums.unrealengine.com/u/Chatouille)\
**Post date:** [July 4, 2022, 7:36am UTC](https://forums.unrealengine.com/t/why-does-unreal-engine-crash-when-i-try-to-do-a-switch-statement/596546/2 "2022-07-04T07:36:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Emaer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/emaer/32/570877_2.png) [@Emaer](https://forums.unrealengine.com/u/Emaer)\
**Post date:** [July 4, 2022, 7:56am UTC](https://forums.unrealengine.com/t/why-does-unreal-engine-crash-when-i-try-to-do-a-switch-statement/596546/3 "2022-07-04T07:56:38Z")

</div>

BTW your code should be shortened to the form:

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![ShadowSprite123](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ShadowSprite123](https://forums.unrealengine.com/u/ShadowSprite123)\
**Post date:** [July 4, 2022, 8:51am UTC](https://forums.unrealengine.com/t/why-does-unreal-engine-crash-when-i-try-to-do-a-switch-statement/596546/4 "2022-07-04T08:51:31Z")

</div>

Thanks for the recommendation I’ll implement it now.

---

<div class="post-metadata">

**Author:** ![ShadowSprite123](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ShadowSprite123](https://forums.unrealengine.com/u/ShadowSprite123)\
**Post date:** [July 4, 2022, 8:53am UTC](https://forums.unrealengine.com/t/why-does-unreal-engine-crash-when-i-try-to-do-a-switch-statement/596546/5 "2022-07-04T08:53:11Z")

</div>

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