'Template Mismatch during attachment' when parenting a BP with my class

Hello,

I have a class that I can drag into a map and use just fine. However if I parent a Blueprint with this class and drag it into a map, I get the following crash. I did some delving and found that at the time of the crash, my capsule component has a RF_Transactional flag set which might be causing problems but I’m not sure what it means. I want to be able to create a blueprint of this class for editing variables and functionality in Blueprint, but is this not the way I should go about things? Thanks, more specific details follow below:

The callstack is this:

 	KernelBase.dll!000007fefd613ca2()	Unknown
>	UE4Editor-Engine.dll!USceneComponent::AttachTo(USceneComponent * Parent, FName InSocketName, EAttachLocation::Type AttachType, bool bWeldSimulatedBodies) Line 914	C++
 	UE4Editor-Engine.dll!USceneComponent::OnRegister() Line 130	C++
 	UE4Editor-Engine.dll!UPrimitiveComponent::OnRegister() Line 305	C++
 	UE4Editor-Engine.dll!UActorComponent::ExecuteRegisterEvents() Line 945	C++
 	UE4Editor-Engine.dll!UActorComponent::RegisterComponentWithWorld(UWorld * InWorld) Line 749	C++
 	UE4Editor-Engine.dll!AActor::IncrementalRegisterComponents(int NumComponentsToRegister) Line 3271	C++
 	UE4Editor-Engine.dll!ULevel::IncrementalUpdateComponents(int NumComponentsToUpdate, bool bRerunConstructionScripts) Line 720	C++
 	UE4Editor-Engine.dll!UWorld::UpdateWorldComponents(bool bRerunConstructionScripts, bool bCurrentLevelOnly) Line 1205	C++
 	UE4Editor-Engine.dll!UWorld::InitializeActorsForPlay(const FURL & InURL, bool bResetTime) Line 2831	C++
 	UE4Editor-Engine.dll!UGameInstance::StartPIEGameInstance(ULocalPlayer * LocalPlayer, bool bInSimulateInEditor, bool bAnyBlueprintErrors, bool bStartInSpectatorMode) Line 195	C++
 	UE4Editor-UnrealEd.dll!UEditorEngine::CreatePIEGameInstance(int PIEInstance, bool bInSimulateInEditor, bool bAnyBlueprintErrors, bool bStartInSpectatorMode, bool bRunAsDedicated, float PIEStartTime) Line 2861	C++
 	UE4Editor-UnrealEd.dll!UEditorEngine::PlayInEditor(UWorld * InWorld, bool bInSimulateInEditor) Line 2212	C++
 	UE4Editor-UnrealEd.dll!UEditorEngine::StartQueuedPlayMapRequest() Line 974	C++
 	UE4Editor-UnrealEd.dll!UEditorEngine::Tick(float DeltaSeconds, bool bIdleMode) Line 1226	C++
 	UE4Editor-UnrealEd.dll!UUnrealEdEngine::Tick(float DeltaSeconds, bool bIdleMode) Line 347	C++
 	UE4Editor.exe!FEngineLoop::Tick() Line 2257	C++
 	UE4Editor.exe!GuardedMain(const wchar_t * CmdLine, HINSTANCE__ * hInInstance, HINSTANCE__ * hPrevInstance, int nCmdShow) Line 142	C++
 	UE4Editor.exe!WinMain(HINSTANCE__ * hInInstance, HINSTANCE__ * hPrevInstance, char * __formal, int nCmdShow) Line 191	C++
 	[External Code]	

The error message I receive is this:

Template Mismatch during attachment. Attaching instanced component to template component. Parent 'CapsuleComponent' Self 'PlayerMesh'

This is my class cpp file:

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

#include "ColossusProject.h"
#include "OwlPawn.h"

// Sets default values
AOwlPawn::AOwlPawn()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	// attach a capsule component as the root component
	UCapsuleComponent* CapsuleComponent = CreateDefaultSubobject<UCapsuleComponent>(TEXT("CapsuleComponent"));
	RootComponent = CapsuleComponent;
	CapsuleComponent->InitCapsuleSize(25.0f, 25.0f);

	// attach a static mesh so the player has something to see
	UStaticMeshComponent* PlayerMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PlayerMesh"));
	PlayerMesh->AttachTo(RootComponent);
	static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
	if (SphereVisualAsset.Succeeded())
	{
		PlayerMesh->SetStaticMesh(SphereVisualAsset.Object);
		PlayerMesh->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f));
	}

	// attach a spring arm and a camera for visuals
	USpringArmComponent* SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
	SpringArm->AttachTo(RootComponent);
	SpringArm->SetRelativeRotation(FRotator(-15.0f, 0.0f, 0.0f));
	SpringArm->TargetArmLength = 400.0f;

	// attach a camera to the spring arm
	UCameraComponent* Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("PlayerCamera"));
	Camera->AttachTo(SpringArm, USpringArmComponent::SocketName);

	// attach a movement component so we can move
	PawnMovementComponent = CreateDefaultSubobject<UOwlPawnMovementComponent>(TEXT("PlayerMovement"));
	PawnMovementComponent->UpdatedComponent = RootComponent;

	// automatically take control of this pawn
	AutoPossessPlayer = EAutoReceiveInput::Player0;
}

// Called when the game starts or when spawned
void AOwlPawn::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AOwlPawn::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

}

// Called to bind functionality to input
void AOwlPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	Super::SetupPlayerInputComponent(InputComponent);

	// set up keybindings
	InputComponent->BindAxis(TEXT("MoveRight"), this, &AOwlPawn::MoveRight);
	InputComponent->BindAxis(TEXT("MoveForward"), this, &AOwlPawn::MoveForward);
}

// Moves the player to the right
void AOwlPawn::MoveRight(float AxisValue)
{
	if (PawnMovementComponent && AxisValue != 0.0f)
	{
		PawnMovementComponent->AddInputVector(GetActorRightVector() * AxisValue);
	}
}

// Moves the player forward
void AOwlPawn::MoveForward(float AxisValue)
{
	if (PawnMovementComponent && AxisValue != 0.0f)
	{
		PawnMovementComponent->AddInputVector(GetActorForwardVector() * AxisValue);
	}
}

Kept up research, and spawning this class instead through a PlayerStart works fine for those who run into this issue. I’d still like to know why this is an issue though.

I was getting this issue as well. After looked a bit at the generated code and doing some tinkering, I think I found out what was causing this. I think you need to make your CapsuleComponent a UProperty like this:

UPROPERTY(Category = Private, VisibleDefaultsOnly)
	class USceneComponent* SceneComponent;

If I comment out the UPROPERTY, then I get the same error. So, it seems that if you are attaching a component to something, then that something should be a UPROPERTY.

Let me know if this changes anything. I’m curious to know if this works for other people than me.

Hi! Looks like I found the issue here. If you use just CreateDefaultSubobject<>() while creating your component it causes it to be garbage-collected, so engine just deletes it and you get the crash (I had it myself for my code). Looks like you should use the UPROPERTY() tag before your component declaration, just like BegKev said before (before ALL of the components). With this tag included, your object is now properly initialized and don’t get “garbage-collected”. It worked for me.

I found (more detailed) explanation of it here: (thanks to cmartel)

Pressing Asset Actions → Reload helped me to fix this problem