Download

How to modify the DefaultSceneRoot Component of an Actor?

Hello, I’m trying to change the DefaultSceneRoot Component by a SkeletalMesh Component. However I didn’t find the way to change this in C++. I tried this:

.h file:



UPROPERTY(VisibleAnywhere)
     class USkeletalMesh* SkeletalMesh;


.cpp file:



AActor::AActor()
{
RootComponent = SkeletalMesh;
}


I have an error: "Impossible to convert USkeletalMesh in USceneComponent"
I know that it is possible with Blueprints. I just have to Add SkeletalMesh Component and drag/drop on DefaultSceneRoot Component to change it. But Is there a way to change it in C++?

Thanks in advance for your help.

You need to include the header for SkeletalMeshComponent in the .cpp file - it’s complaning because it doesn’t know what it is, therefore doesn’t know it’s a scene component.

I added the include for SkeletalMeshComponent but I have always the same error. I don’t understand why it can’t convert SkeletalMesh in SceneComponent because a SkeletalMesh is a SceneComponent.

.h file:



UPROPERTY(VisibleAnywhere)
class USkeletalMesh* SkeletalMesh;


.cpp file:



#include "Components/SkeletalMeshComponent.h"

AActor::AActor()
{
SkeletalMesh = CreateDefaultSubobject<USkeletalMesh>(TEXT("SkeletalMesh"));
RootComponent = SkeletalMesh;
}


I find the problem. I used USkeletalMesh rather than USkeletalMeshComponent.