How to properly attach scene component to other scene component in c++ constructor

Hi.
I got pretty simple question but I can’t manage to make it work so here is the code and motivation.

I want to make scene component that will have some collision shape and some functionality (lets call it Interactor). I want to be able to switch shapes on different derrived classes so I’m using UShapeComponent and initialize it with USphereComponent as a default one. Then I add my cpp version of Interactor to blueprint actor (lets call it User) and it works perfectly. Problem starts when I create BP version of Interactor. Then when I add BP_Interactor to User I got message
Ensure condition failed: false [File:D:\Build++UE4\Sync\Engine\Source\Runtime\Engine\Private\Components\SceneComponent.cpp] [Line: 1854]
Template Mismatch during attachment. Attaching instanced component to template component. Parent ‘BP_Interactor’ (Owner ‘None’) Self ‘InteractorCollision’ (Owner ‘BP_User’). and the attachment simply doesn’t work at all. I can move my Interactor component freerly and the InteractorCollision does not follows.

I’ve noticed that when I delete UPROPERTY(EditDefaultsOnly) from InteractorCollision attachments work well but I can’t switch shapes anymore so I could use derive straight from USphereCollision and get same result.

Header

UCLASS(Blueprintable,ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class ESCAPEROOM_API UInteractorComponent: public USceneComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UInteractorComponent();

protected:
	// Called when the game starts
	virtual void BeginPlay() override;

	UPROPERTY(EditDefaultsOnly)
	UShapeComponent* InteractorCollision;
};

and here is CPP

// Sets default values for this component's properties
UInteractorComponent::UInteractorComponent()
{
	InteractorCollision = CreateDefaultSubobject<USphereComponent>(TEXT("InteractorCollision"));		
	InteractorCollision->SetupAttachment(this);
}

So short recap. How to create and attach one scene component inside other one’s construction script?

I have the same question and the only solution I found isn’t good enough but it works. You can use this check to attach to parent.


if (!this->IsTemplate())
{
  InteractorCollision->SetupAttachment(this);
}

In this way you could see InteractorCollision unattached of UInteractorComponent in Asset editor, but in fact it would be attached. If this effect looks confusing, you can put default creation inside brackets too. But if you do it, you would have to check if UInteractorComponent's "this" is template every time you use it inside itself (because otherwise InteractorCollision wouldn't be created in the first place). From what I found (and I am not sertain) this problem occurs because of CDO system inside unreal. For uclass to be referenced, it should be constructed as template (like special variable available to the whole project), which has some default variables and so on. But when uclass object is created - it's another story, it's not a template anymore, it's a normal object, which has scope and so on.

Please, correct me if I am wrong, because I am also new in Unreal Engine!

Thank you, the explanation matches with what I found out when researching.
Your fix worked for me in 4.23. Thank you!
For anyone still having problems: remember to do a nullptr check everywhere the component is used. Even in other classes.

thanks your experience,it’s worked!!