Download

Crash on AttachRootComponentTo for spawned actor

I am a bit confused on how to check what is wrong with a spawned actor before using it. I believe I have spawned the actor correctly I don’t know what to do other than to check if its NULL. As you can see I’m trying to spawn a new version of the Weapon but it crashes on AttachRootComponent. Anyone got any ideas to help me out?

As a side note: Not sure how people handle these situations since UE4 doesn’t have try and catch blocks nor return codes. How are blueprints catching errors?


void AVRPawn::OnCollisionHand(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult, EHand Hand)
{
	AWeapon* Weapon = Cast<AWeapon>(OtherActor);
	if (Weapon)
	{
		// Get Current Weapon and Motion controller for the correct hand
		AWeapon* CurrentWeapon = Hand == EHand::Left ? CurrentWeaponLeft : CurrentWeaponRight;
		UMotionControllerComponent* MotionController = (Hand == EHand::Left) ? MotionControllerLeft : MotionControllerRight;

		// Setup Spawn Parameters
		FActorSpawnParameters SpawnParams;
		SpawnParams.Owner = this;
		SpawnParams.Instigator = Instigator;

		// Spawn another weapon of that type
		AWeapon* Spawner = GetWorld()->SpawnActor<AWeapon>(Weapon->GetClass(), SpawnParams);

		// Option A: Works Fine but isn't what I want
		Weapon->AttachRootComponentTo(MotionController);

		if (Spawner && MotionController)
		{
			// Option B: Crashes
			Spawner->AttachRootComponentTo(MotionController);
		}
	}

}

Look at the stack trace after the crash (the grey window that says Send error report for UE4) it will tell you the cause and exact line in your code causing the crash.

Normally it would be in that window but in this case it never gets there it just freezes and then closes.

strange. You should then check the log files in projectname/saved/logs folder sort files by time to find the latest log file

also try using simple AttachTo function instead of AttachRootComponentTo and see if there is different behaviour