Weapon Not Attaching To Character From Inventory

I have been following the weapon essential tutorial series on youtube, and even have tried doing it my own way, and for the life of me I can not get this to work.

I able to spawn a weapon into the world an attach to the actors socket, however whenever I try to spawn it from the inventory, the weapon is spawned into the world and functions but refuses to attach to the socket.

I have a USphereComponent to detect collision with weapons:

void AThirdPersonCharacter::OnOverlapBegin(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	//If we have collided with a pickup
	APistolWeaponPickup *Pistol = Cast<APistolWeaponPickup>(OtherActor);
	if (Pistol)
	{
		Inventory[1] = Pistol->GetClass();
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Picked up pistol");
		Pistol->Destroy();
	}

	AAssaultRifleWeaponPickup *Rifle = Cast<AAssaultRifleWeaponPickup>(OtherActor);
	if (Rifle)
	{
		Inventory[0] = Rifle->GetClass();
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Picked up rifle");
		Rifle->Destroy();
	}
}

Everything appears to be functioning normal. But then I have this spaghetti madness which pulls the weapon stored in the first index of the TArray.

 void AThirdPersonCharacter::EquipPrimaryWeapon()
{
	//Equip our primary weapons
	GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Trying to equip");
	FActorSpawnParameters SpawnParams;
	SpawnParams.Owner = this;
	SpawnParams.Instigator = Instigator;
	AWeaponPickup *Spawner = GetWorld()->SpawnActor<AWeaponPickup>(Inventory[0], SpawnParams);
	if (Spawner)
	{
		if (CurrentWeapon != NULL)
		{
			for (int32 i = 0; i < 3; i++)
			{
				if (Inventory[i] != NULL && Inventory[i]->GetDefaultObject<AWeaponPickup>()->WeaponConfig.WeaponName == CurrentWeapon->WeaponConfig.WeaponName)
				{
					Inventory[i] = NULL;
					Inventory[i] = CurrentWeapon->GetClass();
				}
			}
			CurrentWeapon->Destroy();
			Spawner->CollisionSphere->SetCollisionEnabled(ECollisionEnabled::NoCollision);
			Spawner->AttachRootComponentTo(GetMesh(), "RifleSocket", EAttachLocation::SnapToTarget);
			CurrentWeapon = Spawner;
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, Spawner->WeaponConfig.WeaponName);

		}
		else
		{
			Spawner->CollisionSphere->SetCollisionEnabled(ECollisionEnabled::NoCollision);
			Spawner->AttachRootComponentTo(GetMesh(), "RifleSocket", EAttachLocation::SnapToTarget);
			CurrentWeapon = Spawner;
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, Spawner->WeaponConfig.WeaponName);
		}
	}
}

My TArray in my header of defined in the following way:

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Inventory)
		TArray<TSubclassOf<AWeaponPickup>> Inventory;

Again the issue is not the weapon not spawning but the weapon not attaching to the character’s socket when being spawned from an array. I have even tried via blueprint and it failed. Not sure if its a bug or something obvious staring me in the face.

Also I made sure the socket is correct!

The first thing that jumps to my mind is that AttachRootComponentTo only works on components that have been registered. Since you’re spawning the actor, it’s possible that its components haven’t been registered. I’m also not entirely sure this is the right function to use in the first place.

I would try instead:

Spawner()->GetMesh()->AttachTo(GetMesh(), "RifleSocket", EAttachLocation::SnapToTarget);

(typed in, never compiled, caveat emptor) :slight_smile:

Have you tried doing any of the following in your blueprint?

Thanks for the suggestion but after many trials of deleting or adding code to find the cause, I found out it was because my weapon pickup class had its own uspherecomponent on overlap event which were conflicting the player’s collection sphere. So I removed the code that was in the weapon’s overlap events, and it work using the code you posted here. Thanks for your help!