RootComponent change based on UPROPERTY

So i have this code:


if (ItemRow->StaticMesh)
{
	StaticMesh->SetStaticMesh(ItemRow->StaticMesh);
	StaticMesh->SetCollisionProfileName(BaseCollisionProfile);
	RootComponent = StaticMesh;
}
else if (ItemRow->SkeletalMesh)
{
	SkeletalMesh->SetSkeletalMesh(ItemRow->SkeletalMesh);
	SkeletalMesh->SetCollisionProfileName(BaseCollisionProfile);
	RootComponent = SkeletalMesh;
}

In editor it works ok:

273923-root1.png

However in Pie it is broken:

Ok, some more info. If I use ItemRow with StaticMesh - it works properly (because StaticMesh is my default root). However when I drop item from content to lvl (and SkeletalMesh as base) it snaps actor to Vector::Zero - and change root to skeletal mesh. But when i hit play my skeletal mesh is going invisible and hierarchy is back to normal (with StaticMesh as root).

As error states, do StaticMesh is attach to something? Also please provide more information, where do you doing this, how oyu initialize those components, can you show other attachment operation and where they are?

I am doing it in functions “OnConstruction” and “PostEditChangeProperty” - only when property “ItemRow” changed.

I am creating objects like that (in constructor):

273930-constructor.png

And I am changing attachments only in code which I pasted earlier.

I just want to change my root from UStaticMeshComponent to USkeletalMesh component. That’s all.

Ok before making any component a root component, detach it first from anything with this function, this should satisfy that error:

Nothing changed. I think that it is not switching RootComponent properly (but SetRootComponent is returning true)

I managed to fix this. Correct answer:

USceneComponent* oldRoot = RootComponent;
RootComponent->DetachFromComponent(FDetachmentTransformrRules asd);
RootComponent = nullptr;
SetRootComponent(newRootComponent);
oldRoot->AttachToComponent(newRootComponent, FAttachmentTransformRules asd);