# UE5 Template Mismatch During Attaching of Instanced Component

**URL:** <https://forums.unrealengine.com/t/ue5-template-mismatch-during-attaching-of-instanced-component/1510733>\
**Category:** Programming & Scripting\
**Tags:** UE-5, unreal-engine, question, CPP\
**Created:** [December 9, 2023, 3:28pm UTC](https://forums.unrealengine.com/t/ue5-template-mismatch-during-attaching-of-instanced-component/1510733 "2023-12-09T15:28:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Volratholave](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/volratholave/32/474936_2.png) [@Volratholave](https://forums.unrealengine.com/u/Volratholave)\
**Post date:** [December 9, 2023, 3:28pm UTC](https://forums.unrealengine.com/t/ue5-template-mismatch-during-attaching-of-instanced-component/1510733/1 "2023-12-09T15:28:15Z")

</div>

Hello Unreal Engine Community,

I’m encountering a problem with my Unreal Engine 5 project and could really use some help. I have a Blueprint named `BP_VRPawn` which is derived from a C++ class `AVRPawn`. The main issue is that the Skeletal Mesh representing the hands is not being attached to the MotionControllers. Another issue is that the first time I open the folder containing `BP_VRPawn`, or the first time I open the Blueprint itself, the Visual Studio debugger stops, and I receive the following log error:

```auto
[2023-12-09T15:00:14.495Z]LogOutputDevice: Error: Ensure condition failed: false [File:D:\build\++UE5\Sync\Engine\Source\Runtime\Engine\Private\Components\SceneComponent.cpp] [Line: 2012] 
[2023-12-09T15:00:14.495Z]LogOutputDevice: Error: Template Mismatch during attachment. Attaching instanced component to template component. Parent 'RightHandComponent' (Owner 'Default__BP_VRPawn_C') Self 'MotionController' (Owner 'BP_VRPawn_C_0').

```

No other changes have been made to `BP_VRPawn` other than setting Meshes to visualize the HMD, controllers and hands.

Here’s an overview of my class setup in C++:

`UVRHandComponent` is a custom component that holds a `MotionController` and a `HandMesh`. Its code is structured as follows:

VRHandComponent.h:

```cpp
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class PROJECT_SHIBAINU_API UVRHandComponent : public USceneComponent
{
    GENERATED_BODY()

public:
    UVRHandComponent();
    
    // Function to set the tracking source
    UFUNCTION(BlueprintCallable, Category = "VR")
    void SetTrackingSource(const FName NewSource);
    
protected:
    virtual void BeginPlay() override;
    
    UPROPERTY(VisibleAnywhere)
    UMotionControllerComponent* MotionController;

    UPROPERTY(VisibleAnywhere)
    USkeletalMeshComponent* HandMesh;
};

```

VRHandComponent.cpp:

```cpp
UVRHandComponent::UVRHandComponent()
{
    MotionController = CreateDefaultSubobject<UMotionControllerComponent>(TEXT("MotionController"));
    MotionController->SetupAttachment(this);

    HandMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("HandMesh"));
    HandMesh->SetupAttachment(MotionController);

    HandMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}

void UVRHandComponent::SetTrackingSource(const FName NewSource)
{
    if (MotionController)
    {
        MotionController->SetTrackingMotionSource(NewSource);
    }
}

```

The `AVRPawn` inherits from `ACharacter` and builds upon the `UVRHandComponent`s:

VRPawn.h:

```cpp
UCLASS()
class PROJECT_SHIBAINU_API AVRPawn : public ACharacter
{
    GENERATED_BODY()

public:
    AVRPawn();

protected:
    virtual void BeginPlay() override;
    
    UPROPERTY(VisibleAnywhere)
    UCameraComponent* VRPlayerCamera;
    
    UPROPERTY(VisibleAnywhere)
    UStaticMeshComponent* VRHeadsetMesh;
    
    UPROPERTY(VisibleAnywhere)
    UVRHandComponent* LeftHandComponent;
    
    UPROPERTY(VisibleAnywhere)
    UVRHandComponent* RightHandComponent;

    // Rest of the code...
};

```

`VRPawn.cpp`:

```cpp
AVRPawn::AVRPawn()
{
    // Set this pawn to call Tick() every frame.
    PrimaryActorTick.bCanEverTick = true;

    // Instantiate a new SceneComponent and set it as the root component
    RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));

    // Instantiate VROrigin and attach it to RootComponent
    VROrigin = CreateDefaultSubobject<USceneComponent>(TEXT("VROrigin"));
    VROrigin->SetupAttachment(RootComponent);

    // Disable collision for the capsule component
    GetCapsuleComponent()->SetCollisionEnabled(ECollisionEnabled::NoCollision);

    // Initialize the camera component for the VR pawn and attach it to VROrigin
    VRPlayerCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("VRPlayerCamera"));
    VRPlayerCamera->SetupAttachment(VROrigin);

    // Initialize the static mesh component to represent the headset
    VRHeadsetMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VRHeadsetMesh"));
    VRHeadsetMesh->SetupAttachment(VRPlayerCamera);

    // Disable collision for the VR headset mesh
    VRHeadsetMesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);

    // Initialize the camera position arrow and attach it to the capsule component
    CameraPositionArrow = CreateDefaultSubobject<UArrowComponent>(TEXT("CameraPositionArrow"));
	CameraPositionArrow->SetupAttachment(GetCapsuleComponent());

    // Initialize the VR hand components
    LeftHandComponent = CreateDefaultSubobject<UVRHandComponent>(TEXT("LeftHandComponent"));
    if (LeftHandComponent)
    {
        LeftHandComponent->SetupAttachment(VROrigin);
        LeftHandComponent->SetTrackingSource(TEXT("LeftGrip"));
    }

    RightHandComponent = CreateDefaultSubobject<UVRHandComponent>(TEXT("RightHandComponent"));
    if (RightHandComponent)
    {
        RightHandComponent->SetupAttachment(VROrigin);
        RightHandComponent->SetTrackingSource(TEXT("RightGrip"));
    }

}

```

Here are the screenshots provided showing how the `BP_VRPawn` components are set up in the Unreal Engine 5 editor:

 ![VRPawn structure](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/e/6/ae604039bc25ed196fc38108e4503e3af57302da.png)  
 ![changed details](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/1/6/f162f36cfbb50dd40c7829b7caa2c16ac4d1e542.png)

If anyone has experience with this sort of problem or can spot anything in my code that I’m overseeing, your input would be greatly appreciated. Thank you in advance for your help!

Best,

---

<div class="post-metadata">

**Author:** ![Paul\_Bronowski](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Paul\_Bronowski](https://forums.unrealengine.com/u/Paul_Bronowski)\
**Post date:** [March 1, 2024, 5:25pm UTC](https://forums.unrealengine.com/t/ue5-template-mismatch-during-attaching-of-instanced-component/1510733/2 "2024-03-01T17:25:36Z")

</div>

This worked for me…

```auto
void UMyComponent::OnRegister()
{
    Super::OnRegister();

    // TODO: BUGBUG: Work around the 'Template Mismatch during attachment. Attaching instanced component to template component.' problem.
    // Remove after Epic acknowledges and fixes this long-standing bug or provides guidance.
    // The constructor-initialized components, upon non-CDO instantiation/initialization, are getting CDO references for their attached parents, instead of instance references.
    // A work-around...
    // - Use SetupAttachment(), per usual in the constructor to establish the parent-child relationship for CDOs.
    // - Use AttachToComponent() to override those errant CDO references with instance references, in your USceneComponent::OnRegister() override.
    this->MySubComponent1->AttachToComponent(this->GetAttachmentRoot(), FAttachmentTransformRules::KeepRelativeTransform);
    this->MySubComponent2->AttachToComponent(this->MySubComponent1, FAttachmentTransformRules::KeepRelativeTransform);
}
```

---

<div class="post-metadata">

**Author:** ![DrTrev4](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DrTrev4](https://forums.unrealengine.com/u/DrTrev4)\
**Post date:** [May 25, 2024, 7:20pm UTC](https://forums.unrealengine.com/t/ue5-template-mismatch-during-attaching-of-instanced-component/1510733/3 "2024-05-25T19:20:13Z")

</div>

Thank you so much for this. I found this while trying to solve this issue for myself and it squared me away. I really appreciate it.

---

<div class="post-metadata">

**Author:** ![zero.soul](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/zero.soul/32/958024_2.png) [@zero.soul](https://forums.unrealengine.com/u/zero.soul)\
**Post date:** [September 15, 2024, 8:29pm UTC](https://forums.unrealengine.com/t/ue5-template-mismatch-during-attaching-of-instanced-component/1510733/4 "2024-09-15T20:29:09Z")

</div>

`this` refers to the CDO in the constructor, not the instance, causing the error.

Basically, actor components support nesting. But you can only execute the nested logic within the actor, not within the component.

Here is my solution, I create a note :  
[https://kisspread.github.io/notes/Basic/painful\_ue/#nesting-actor-components-isnt-exactly-straightforward](https://kisspread.github.io/notes/Basic/painful_ue/#nesting-actor-components-isnt-exactly-straightforward)

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [October 23, 2024, 9:00pm UTC](https://forums.unrealengine.com/t/ue5-template-mismatch-during-attaching-of-instanced-component/1510733/5 "2024-10-23T21:00:42Z")

</div>

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