UE4 Crashes when a Line Trace won't hit anything

Hello.
I made a weapon that fires using Line Trace. It works well, but every time it doesn’t hit anything it crashes my engine.
Here’s my Line Trace code:

    	FCollisionQueryParams FireTraceParams = FCollisionQueryParams(FName(TEXT("FireTrace")), true, this);
    	FireTraceParams.bTraceComplex = true;
    	FireTraceParams.bTraceAsyncScene = true;
    	FireTraceParams.bReturnPhysicalMaterial = false;
    	FHitResult FireHit(ForceInit);
    
    	UWorld* const World = GetWorld();
    	FVector StartLocation;
    	StartLocation = GetActorLocation();
    	FVector EndLocation;
    	EndLocation = StartLocation + PlayerCamera->GetForwardVector() * 100000000.0f;
    
    	if (World && ShootDelay <= 0.0f)
    	{
    		World->LineTraceSingleByChannel(FireHit, StartLocation, EndLocation, ECC_Pawn, FireTraceParams);
    
    		UGameplayStatics::SpawnEmitterAtLocation(World, ProjectileParticleSystem, FireHit.Location);
    
    		ImpactVector = EndLocation / 10000.0f;
    		if (FireHit.GetComponent()->IsSimulatingPhysics()) FireHit.GetComponent()->AddImpulseAtLocation(ImpactVector, FireHit.ImpactPoint);
    		ShootDelay = 30.0f;
    
    		/*DrawDebugLine(
    			GetWorld(),
    			StartLocation,
    			FireHit.Location,
    			FColor(255, 0, 0),
    			false, -1, 0,
    			12.333
    		);*/
    	}

If you know why, then please answer.

LineTraceSingleByChannel returns true if it hit something and false otherwise. Right now you’re just blindly assuming it hits something and then calling “FireHit.GetComponent()” which will return null if it didn’t hit anything. You need to change your code so it’s something like this:

If (World->LineTraceSingleByChannel(....))
{
  // The trace hit something. It's now safe to use the result. 
}

Thank you :slight_smile: