Attaching Mesh to Socket Causing Editor To Crash

Since the 4.5.1 Update, for some reason when i try to overlap my weapon, it crashes the editor. I’m unsure why, but can anyone help me??

.cpp

void ATestCharacter::OnCollision(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{
	AWeapon* Weapon = Cast<AWeapon>(OtherActor);
	if (OtherActor == Weapon)
	{
		Inventory.Add(Weapon->GetClass());
		FString InvName = Inventory[0]->GetName();
		GEngine->AddOnScreenDebugMessage(-1, 4.f, FColor::Green, InvName);
		SpawnWeapon(Inventory[0]);

		Weapon->Destroy();
	}
}

void ATestCharacter::SpawnWeapon(TSubclassOf<AWeapon> Weapon)
{
	FActorSpawnParameters SpawnParams;
	SpawnParams.Owner = this;
	SpawnParams.Instigator = Instigator;
	AWeapon *Spawner = GetWorld()->SpawnActor<AWeapon>(Weapon, SpawnParams);
	if (Spawner)
	{
		Spawner->AttachRootComponentTo(Mesh, FName(TEXT("Weapon_Socket")), EAttachLocation::SnapToTarget); //////// THIS PART DOES NOT WORK, CAUSES CRASH
	}
}

.h

void SpawnWeapon(TSubclassOf<AWeapon> Weapon);

UPROPERTY(EditDefaultsOnly, Category = Inventory)
TArray<TSubclassOf<AWeapon>> Inventory;

To ensure a few things, Yes I have a socket named Weapon_Socket on my character skeletal mesh, I ensured the Collision checks are working… Spawning the mesh is fine, but when I try to Attach the mesh to the socket on the Character, It crashes the Editor.

Try turning CCD Off

Your crash sounds highly related to the one that I spent a day debugging for Solus

Turns out it was CCD (Thanks Ori!)

Try turning CCD off ! (Continous Collision Detection)

Make sure to check your physics asset that no bodies are using CCD

Rama

Rama,

I have tried a more simplified situation. I ensured that the CCD is off on all of the components. both the character and the object that is being touched as well… It’s still crashing the engine :confused: . here’s the basic code for the situation.

weapon.h
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Actor.h"
#include "Weapon.generated.h"

/**
 * 
 */
UCLASS()
class TESTER_API AWeapon : public AActor
{
	GENERATED_UCLASS_BODY()

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Mesh)
	TSubobjectPtr<UStaticMeshComponent> Mesh;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision)
	TSubobjectPtr<UBoxComponent> CollisionComp;
	
};

.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "Tester.h"
#include "Weapon.h"


AWeapon::AWeapon(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	Mesh = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, "Mesh");
	RootComponent = Mesh;

	CollisionComp = PCIP.CreateDefaultSubobject<UBoxComponent>(this, "CollisionComp");
	CollisionComp->AttachTo(RootComponent);
}

character .h

#pragma once
#include “Weapon.h”
#include “GameFramework/Character.h”
#include “TesterCharacter.generated.h”

UCLASS(config=Game)
class ATesterCharacter : public ACharacter
{
GENERATED_UCLASS_BODY()

/** Camera boom positioning the camera behind the character */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
TSubobjectPtr<class USpringArmComponent> CameraBoom;

/** Follow camera */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
TSubobjectPtr<class UCameraComponent> FollowCamera;

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision)
TSubobjectPtr<UBoxComponent> CollisionComp;

UFUNCTION()
void OnCollision(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult);

void SpawnWeapon(TSubclassOf<class AWeapon> Weapon);

};

.cpp

 void ATesterCharacter::OnCollision(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
    {
    	AWeapon* Weapon = Cast<AWeapon>(OtherActor);
    	if (Weapon)
    	{
    		GEngine->AddOnScreenDebugMessage(-1, 4.f, FColor::Green, "HELLO IM A WEAPON!!");
    		SpawnWeapon(Weapon->GetClass());
    		Weapon->Destroy();
    	}
    }
    
    void ATesterCharacter::SpawnWeapon(TSubclassOf<class AWeapon> Weapon)
    {
    	AWeapon* Spawner = GetWorld()->SpawnActor<AWeapon>(Weapon);
    	if (Spawner)
    	{
    		GEngine->AddOnScreenDebugMessage(-1, 4.f, FColor::Green, "I SPAWNED");
    		Spawner->AttachRootComponentTo(Mesh, "Weapon_Socket", EAttachLocation::SnapToTarget);
    	}
    }

most of the comments can only fit soo much… but relatively, It’s slimmer, but the same situation… The object spawns just fine, just wont attach to socket even the SetLocation wont work… and i turned off the CCD by unchecking the Use CCD checkmark in the collisions Here

Still at a standstill figuring out why it’s not working :frowning:

Update - I tried putting the socket farther away from the mesh, and it works just fine… but I want the mesh to be attached closer because it needs to be attached to the hand Dx. even with the CCD off, it still crashes :frowning:

Any new news about this bug? :frowning: because my only temporary solution I have for now is doing this to the mesh before attaching it… I had to delete the UBoxComponent in general right when it spawned

void AWeaponEssentialsCharacter::SpawnWeapon(TSubclassOf<AWeapon> Weapon)
{
	FActorSpawnParameters SpawnParams;
	SpawnParams.Owner = this;
	SpawnParams.Instigator = Instigator;
	AWeapon *Spawner = GetWorld()->SpawnActor<AWeapon>(Weapon);
	if (Spawner)
	{
		Spawner->CollisionComp->DestroyComponent();//WITHOUT THIS, THE EDITOR WILL CRASH!!!!!
		Spawner->AttachRootComponentTo(Mesh, FName(TEXT("Weapon_Socket")), EAttachLocation::SnapToTarget);
	}
}

KEEP IN MIND! I switched my mesh to a skeletal mesh… static mesh still has this issue!!!

UPDATE:
if you set the collision to NoCollision when you spawn the Static mesh, It will work too… It’s the temporary fix for now I believe :frowning: