# Why is Surface Type returning NULL?

**URL:** <https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855>\
**Category:** C++\
**Tags:** Raycast, unreal-engine, UE4, question\
**Created:** [July 5, 2022, 8:31pm UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855 "2022-07-05T20:31:57Z")\
**Posts on this page:** 16\
**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 5, 2022, 8:31pm UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855/1 "2022-07-05T20:31:57Z")

</div>

So I’ve managed to fix the crash happening with the switch on the Surface Type but now the Surface Type is returning NULL and I don’t know how to fix it.  
Any Ideas?

**Player.CPP**

```auto
//Target is the player reference and Hit is the FHitResult info
//Health is the health component we're accessing using the player reference and HP is teh health value stored in the health component
switch (UGameplayStatics::GetSurfaceType(Hit))
		{
		case SurfaceType1:
			UGameplayStatics::ApplyPointDamage(Target, 50.0f, Hit.TraceStart, Hit, GetInstigatorController(), this, UDamageType::StaticClass());
			GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, FString::SanitizeFloat(Target->Health->HP));
			GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, FString::Printf(TEXT("SurfaceType: %s"), UGameplayStatics::GetSurfaceType(Hit)));
			break;
		case SurfaceType2:
			UGameplayStatics::ApplyPointDamage(Target, 25.0f, Hit.TraceStart, Hit, GetInstigatorController(), this, UDamageType::StaticClass());
			GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, FString::SanitizeFloat(Target->Health->HP));
			GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, FString::Printf(TEXT("SurfaceType: %s"), UGameplayStatics::GetSurfaceType(Hit)));
			break;
		default:
			UGameplayStatics::ApplyPointDamage(Target, 5.0f, Hit.TraceStart, Hit, GetInstigatorController(), this, UDamageType::StaticClass());
			GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, FString::SanitizeFloat(Target->Health->HP));
			GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, FString::Printf(TEXT("SurfaceType: %s"), UGameplayStatics::GetSurfaceType(Hit)));
		}

```

Currently when the Surface Type is returned in the editor I get: (null), meaning in the switch statement its executing default.

And thanks in advance.

---

<div class="post-metadata">

**Author:** ![barracius-AOne](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/barracius-aone/32/548049_2.png) [@barracius-AOne](https://forums.unrealengine.com/u/barracius-AOne)\
**Post date:** [July 5, 2022, 9:46pm UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855/2 "2022-07-05T21:46:13Z")

</div>

Hello!

I think the issue you are facing is with how to make a switch statement with an enum. So for the Surface Type, what it returns is a EPhysicalSurface ([here is the doc](https://docs.unrealengine.com/4.27/en-US/API/Runtime/PhysicsCore/Chaos/EPhysicalSurface/)). So what you need for a switch statement with an enum is to set it up like this:

```auto
switch(UGameplayStatics::GetSurfaceType(Hit))
{
    case EPhysicalSurface::SurfaceType1:
         // something
         break;
    case EPhysicalSurface::SurfaceType2:
        // something
        break;
    default:
        // default something
}

```

Edit: Nevermind, that was not the issue. What I would try is to check what is `UGameplayStatics::GetSurfaceType(Hit)` giving you. That could be done by adding

```auto
EPhysicalSurface TempSurface = UGameplayStatics::GetSurfaceType(Hit);
switch(TempSurface)
{
  // same as before
}

```

and place a breakpoint on your editor on the switch statement, your editor should tell you the value of TempSurface. If by any means you can’t place a breakpoint, try adding UE\_LOGs on default case, with the TempSurface as a argument.

`UE_LOG(LogTemp, Log, TEXT("The surface is %s"), *UEnum::GetValueAsString(TempSurface));`

---

<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 6, 2022, 3:55am UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855/3 "2022-07-06T03:55:04Z")

</div>

I did as you said and it says TempSurface is SurfaceType\_Default even though I’m hitting a surface that’s suppose to be SurfaceType1 or SurfaceType2 both return SurfaceType\_Default which is weird.  
Any ideas?

And thanks for your reply!

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [July 6, 2022, 7:37am UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855/4 "2022-07-06T07:37:09Z")

</div>

You have to make sure the collision query parameters had the `bReturnPhysicalMaterial` set to true, otherwise it will always be the default.

If using complex collision, the material is specified by the material shader. If using simple collision, it’s specified by the collision body.

---

<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 6, 2022, 9:04am UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855/5 "2022-07-06T09:04:33Z")

</div>

Thanks for the reply but that didn’t fix the issue. It still returns the default surface type. Also what do you mean by the second statement(about the collision)?

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [July 6, 2022, 9:09am UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855/6 "2022-07-06T09:09:34Z")

</div>

Well you need to actually apply a phyiscal material to the surface.

If it’s a complex collision query, you are querying the mesh triangles so you need to make sure all the materials have a physical material set.

If you are querying simple collision, the collision rigid bodies need physical materials applied. For skeletal meshes this is done in the physics asset, for other componets this is done in the component details and/or the mesh asset. The default is none.

This is also somewhat dependant on the Collision Mode of the object, e.g. “Default, UseSimpleAsComplex etc…” - since that may change what you are querying.

---

<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 6, 2022, 9:12am UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855/7 "2022-07-06T09:12:50Z")

</div>

What would you suggest I change in the physics asset?

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [July 6, 2022, 9:13am UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855/8 "2022-07-06T09:13:36Z")

</div>

Click through every collision body, and apply a physical material to it.

---

<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 6, 2022, 9:13am UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855/9 "2022-07-06T09:13:47Z")

</div>

I did but it still returns null.

---

<div class="post-metadata">

**Author:** ![barracius-AOne](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/barracius-aone/32/548049_2.png) [@barracius-AOne](https://forums.unrealengine.com/u/barracius-AOne)\
**Post date:** [July 6, 2022, 4:29pm UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855/10 "2022-07-06T16:29:51Z")

</div>

This may sound silly, but did you edit the surface types on `Project Settings -> Physics -> Physical Surface` ?

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/3/1/b31300b6376167c9166d1b63697980820ded7422.jpeg)

Looks like that has to be set up in order for it to work.

---

<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 6, 2022, 8:00pm UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855/11 "2022-07-06T20:00:41Z")

</div>

Yes and for some reason it still returns the default surface type.

---

<div class="post-metadata">

**Author:** ![barracius-AOne](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/barracius-aone/32/548049_2.png) [@barracius-AOne](https://forums.unrealengine.com/u/barracius-AOne)\
**Post date:** [July 6, 2022, 9:18pm UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855/12 "2022-07-06T21:18:20Z")

</div>

The only things I can think of are:

- Maybe the surface is not set with that surface type.
- The raycast is not hitting the surface that you want, you could debugdrawline that raycast to see it on the viewport.

---

<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 7, 2022, 6:14am UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855/13 "2022-07-07T06:14:42Z")

</div>

I have set the surface type which is still weird that it’s still returning the default one and I have checked with a debug line and it is hitting; and, on top of that the way I set it up it is returning the actor its hitting(done with a debug message). Any ideas what’s going on?

---

<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 8, 2022, 3:05am UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855/14 "2022-07-08T03:05:46Z")

</div>

I figured out why it wasn’t working! The only problem now is that I don’t know how to fix it. What was happening was that it was returning the default surface type because it wasn’t colliding with the mesh but with the capsule component. Anyone know how to fix this?

---

<div class="post-metadata">

**Author:** ![barracius-AOne](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/barracius-aone/32/548049_2.png) [@barracius-AOne](https://forums.unrealengine.com/u/barracius-AOne)\
**Post date:** [July 8, 2022, 3:09pm UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855/15 "2022-07-08T15:09:03Z")

</div>

Maybe you could assign different collision channels for that Raycast/Surface and another for the Capsule Component. [This](https://docs.unrealengine.com/4.27/en-US/InteractiveExperiences/Tracing/Overview/) may help you with that

---

<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 9, 2022, 12:25am UTC](https://forums.unrealengine.com/t/why-is-surface-type-returning-null/597855/16 "2022-07-09T00:25:58Z")

</div>

I fixed it! The problem was that I was using Use Complex Collision as Simple instead of Use Simple Collision as Complex in the physics asset.
