Attaching To Meshes Via Sockets - Editor crashes on play

Hi, I’m following along and as far as I can tell I’ve copied your code exactly. Everything works right up until actually attaching the rifle to the socket. If I comment those two lines out everything works, but I put them back in and it’s a hard crash every time.

This is my code:

void AShooterCharacter::BeginPlay()
{
	Super::BeginPlay();
	
	GetWorld()->SpawnActor<AGun>(GunClass);
	GetMesh()->HideBoneByName(TEXT("weapon_r"), EPhysBodyOp::PBO_None);
	Gun->AttachToComponent(GetMesh(), FAttachmentTransformRules::KeepRelativeTransform, TEXT("WeaponSocket"));
	Gun->SetOwner(this);
}

What does the crash log say in unreal. The code does look fine to me.

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000140

UE4Editor_Engine
UE4Editor_SimpleShooter_7164!AShooterCharacter::BeginPlay() [c:\users\capta\documents\unreal projects\simpleshooter\source\simpleshooter\shootercharacter.cpp:23]
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
kernel32
ntdll

Line 23 is

Gun->SetOwner(this);

Try adding

Gun = GetWorld()->SpawnActor<AWeaponBase>(GunClass);
4 Likes

Thanks so much! for my class names that means

	Gun = GetWorld()->SpawnActor<AGun>(GunClass);

I must have missed something else previously because that makes sense, Gun wasn’t being assigned a value anywhere else. Thanks!

1 Like

No problem what we are here for haha.

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

Privacy & Terms