UE5 Template Mismatch During Attaching of Instanced Component

This worked for me…

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);
}
2 Likes