[Suggestion] Access Violation

This might be a good place in the course to talk about the dreaded access violation error / crash. In this case, I messed up the if statement and so when I got to the HitActor->GetActorNameOrLabel() part, it tried to access the name from a null reference (when HasHit is false, HitActor = null). Since this is a fairly regular occurrence, if one isn’t careful, and causes a scary crash, it might be worth a short lesson here.

Thanks for the suggestion. I’ve forwarded it to Sam.

I’m getting this access violation every time the sweep hits.

	FCollisionShape sphere = FCollisionShape::MakeSphere(grabRadius);
	//FCollisionShape line=FCollisionShape::LineShape;
	FHitResult hitResult;
	if (GetWorld()->SweepSingleByChannel(hitResult, start, end, FQuat::Identity, ECC_GameTraceChannel2, sphere)) {
		AActor* hitActor = hitResult.GetActor();
		if (&hitActor != NULL) { UE_LOG(LogTemp, Display, TEXT("Object Hit: %s"), *hitActor->GetActorNameOrLabel()); }
	} else {
		UE_LOG(LogTemp, Display, TEXT("Object Hit: none"));
	}
if (&hitActor != NULL) 
{ 
    UE_LOG(LogTemp, Display, TEXT("Object Hit: %s"), *hitActor->GetActorNameOrLabel()); 
}

This is always true as you’re checking the address of the variable that’s on the stack and not what the address that’s stored in the pointer. (remove the &)

whoops thanks that worked

when it’s not printing anything, it’s difficult to figure out what it’s hitting… im not using the crypt raider pack, im using megascans and geometry

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms