Howto add StaticMesh to SceneComponent

USceneComponent apparently has no RootComponent. So I have no idea how to add a StaticMesh to it:

// In the constructor of my custom USceneComponent:
SMC = CreateDefaultSubobject<UStaticMeshComponent>("SMC");
SMC->AttachToComponent(this, FAttachmentTransformRules::KeepRelativeTransform); // This compiles but crashes the editor when selecting an actor that contains this SceneComponent. Normally "this" is "RootComponent".

Or do I have to use a ChildActorComponent instead of a SceneComponent? I can’t really figure out what the difference between the two is. Note that using a ChildActorComponent instead also crashes the editor the same way.

Hey there, SceneComponent and ChildActorComponent are 2 completely different components. SceneComponent is just a simple component with a transform and it’s used to organize other components into a group, to serve as a pivot, or just to be there as a placeholder. ChildActorComponent allows you to add the functionality of an actor into your blueprint, so it will spawn an instance of that actor and make it part of your blueprint. Regarding your issue, a Root Component can be any SceneComponent (or child classes of it) but you are using “this”, “this” is a reference to the actor, not the RootComponent, so you need to do:

SMC->SetupAttachment(RootComponent); // instead of SMC->AttachToComponent(this, FAttachmentTransformRules::KeepRelativeTransform);

Hey there, I see that in that case I indeed need a USceneComponent.

However the reason for me to use this instead of RootComponent is because USceneComponent derived classes have no RootComponent…


leads to this error: Error: ‘RootComponent’: undeclared identifier.

My class inherits directly from USceneComponent. What could I have possibly overlooked? Only actors have RootComponents. I Pressed F12 on an actor RootComponent and came out here (in Actor.h):

// Line 522-523
UPROPERTY(BlueprintGetter=K2_GetRootComponent, Category="Utilities|Transformation")
USceneComponent* RootComponent;

So basically USCeneComponents are ‘RootComponents’. That is why I used this. But UE4 does not like that. So how can I make it work?

Ah ok i thought you were dong that in the actor. Afaik custom components can’t have components inside them, why do you want to do that? Why not create an actor?

I assumed that SceneComponents are better for performance compared to Actors, plus Actors have so much more functionality that I do not need. I only need a translation, rotation and scale thingy for my custom class so the SceneComponent made sense. But if you can’t attach anything to custom SceneComponents… That changes everything. I wish the UE4 documentation would tell me this but the documentation for this engine is pretty bad. Thanks for that info.

Well, in that case it seems that I will have no choice. I must make them all actors (so 7 actors for 1 cube) and attach the 6 actors to the parent using something like this:

MyActor->AttachRootComponentToActor(this); // In parent actor

PrimaryActorTick.bCanEverTick = true; // Disable for performance reasons in the child class (the one that used to be my custom SceneComponent).

Oh I see that I also cannot set the location of the new actors in the parent actor yet because the parent actor apparently is still in 0,0,0 while inside the constructor…

Also I believe that there might be an UE4-bug. The names shown in the label are different from it’s actual name when using Actors instead of SceneComponents:


My original code apparently was good. I have no idea why it crashed before. Probably something else caused a crash at exactly that line. But the code should be good. It is safe to derive from USceneComponent and to use the this-keyword to attach stuff to it and there is no need to manually add a RootComponent.