Using GetOwner() crash the editor

Hello , I’m making a shoot them up in c++ and to know who shoot the projectile I’m using “Owners”, but on any actors when I try to get the projectile owner the editor crash without any errors.
Here is the code :

void Ashooter::OnHit(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::, OtherActor->GetOwner()->GetName());
}

For now I’m only debuging but it still crash , so if you have any ideas why it does this or an other method to do it please tell me.

Hello jak951,

It may be a null pointer issue, where it’s being called on something which doesn’t have an outer. Could you try surrounding that line of code with an if statement that checks to see if OtherActor->GetOwner() is valid so that it’ll avoid calling a null value?

Otherwise, when you say that it crashes with no errors, are you at least getting a crash reporter window? If so, could you give me either the Epic ID or Machine ID listed in that window? That should allow me to look up the crash internally and see the callstack.

probably null pointer
unreal usually crashes cause of it, so you need to check if OtherActor is not null pointer

set if statement

if (OtherActor != nullptr && OtherActor != this && OtherComp !=nullptr)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::, OtherActor->GetOwner()->GetName());
}

Ok I tried with the code you share and it still crash , I really don’t understand why because there is no reasons! Oh , by the way, the editor close itself after the compilation if I add this code ( it close even if I didn’t press play ) :

ACharacter* myCharacter = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);

Were you able to get the Epic ID or Machine ID when the crash occurred? Having those will allow me to look up the cause of the crash, as long as you’re hitting the “Send and Close” or “Send and Restart” button when crashing. I’ll need this information to be able to assist you further, otherwise we don’t know what is causing the crash.

When the engine crash nothing appear , it just close without any warning. Actually I’m trying to reinstall the engine , it might solve the problem.

I solve the problem , it happend because the projectile spawn at the same coordinate as the shooter , so I just add a small offset.

Glad to hear that the problem has been fixed. Have a nice day!