Hello,
Im new to ue4 and im working on a project. I created an actor with c++ class. I want my actor to have 2 separate components (not one attached to the other). If i declare the 2 components in the header file and then in the .cpp file i type: StaticMesh(the name of the first component) = CreateDefaultSubobject(“StaticMeshComponent”); and after that i write the same for the other component(just the name is not the same), it attaches the second one to the first one! Soo my question is how to make them separatley not one attached to the other? Can you give me the code please! Sorry for my bad english!
I’m assuming you’re doing something like this:
// Sets default values
ATestActor::ATestActor()
{
PrimaryActorTick.bCanEverTick = true;
Mesh1 = CreateDefaultSubobject<UStaticMeshComponent>("StaticMeshComponent");
Mesh2 = CreateDefaultSubobject<UStaticMeshComponent>("StaticMeshComponent2");
}
The problem here is that the first component you create will overwrite the “Default root” component. That’s why you see the first mesh become the root component and the second mesh attaches to the first.
If you don’t want either mesh to become the root component, you need to specifically create a component to be the root component.
Here is a code snippet:
// Sets default values
ATestActor::ATestActor()
{
PrimaryActorTick.bCanEverTick = true;
// Scene root
USceneComponent* sceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("DefaultSceneRoot"));
RootComponent = sceneRoot;
Mesh1 = CreateDefaultSubobject<UStaticMeshComponent>("StaticMeshComponent");
Mesh2 = CreateDefaultSubobject<UStaticMeshComponent>("StaticMeshComponent2");
}
And here’s how the components will look in Blueprints: