RootComponent variable for Actors which never had CreateDefaultSubobject invoked is null

This includes actors where the entire hierarchy is defined in BP, like ones that just inherit from a C++ AActor derivative whose constructor never calls CreateDefaultSubobject or sets RootComponent itself.
Having RootComponent be null means that AActor::AttachRootComponentTo will do nothing, which is used in ChildComponentComponent to attach the Child actors to the parent.

Hey rajkosto-

Could you elaborate on exactly what is happening? After setting up the hierarchy for the blueprint (who’s parent was a custom actor class), with DefaultSceneRoot as the root component, I added an instance to the level and when printing the display name of the root component saw DefaultSceneRoot printed to the screen. Where are you seeing the root component being set to null?

Try setting it as a class in a ChildActorComponent, then breakpoint inside UChildActorComponent::CreateChildActor on the ChildActor->AttachRootComponentTo line. Inside that function you will see that RootComponent is nullptr and the Child Actor doesn’t actually get attached.
Worked around this problem by having my native AActor do CreateDefaultSubobject with a USceneComponent and set it as its root in its constructor. The problem only occured when my native AActor had an empty constructor (with FObjectInitializer), and all the hierarchy was set up in the blueprint inheriting it.

I’m not sure I understand what you’re doing. Are you creating a blueprint based on a custom class or adding a custom component to a blueprint? Could you explain your steps to reproduce the null root component as well as post the code that you’ve used in your setup?

UCLASS(Abstract,Blueprintable,Meta = (ChildCanTick))
class AClimbable : public AActor
AClimbable::AClimbable(const FObjectInitializer& PCIP) : Super(PCIP) {}

Then make a blueprint inheriting from it, create a new scene component as the root, named it RootComp, add some other components below it.

Now try and spawn that blueprint as a ChildActorComponent in some other actor. It won’t attach to its parent.

Hey rajkosto-

Here at the steps that I took:

  1. Create new Actor class (using the setup you posted) and compile
  2. Create a blueprint of this class (MyActorBP)
  3. In MyActorBP I added scene component as root component (RootComp) and a capsule component and static mesh component
  4. Created a new blueprint based on Actor (SpawningBP)
  5. In SpanwingBP I was able to add a child actor component and set it to MyActorBP (not sure if this is what you are doing)

Are you referring to creating MyActorBP as a child component during runtime? If so can you post the blueprint setup you use to do so?

Is the child actor properly attached ? What is RootComponent inside that function if you run in debug mode and place a breakpoint where i said ?

Hey rajkosto-

Could you explain how exactly this is affecting you? I put a breakpoint where you indicated and the RootComponent variable in the function was reporting NULL however the ChildActorComponent in the blueprint was setup properly(set MyActorBP as the Child Actor Class). I noticed the same result when attaching a blueprint based on Actor (no code) in the same way. Additionally if I print the components of the child actor components it gives me all of the components of MyActorBP in the correct hierarchy order.

Well if the RootComponent is null in that function…it means it won’t attach.
So try moving around the actor with the child component, the child actor won’t follow.

When moving the SpawningBP instance in the level the child actor component moved along with it. Adding a simple function to move the actor in the SpawningBP blueprint also moved the child actor component during runtime.

Please explain how this issue is affecting you so I have an understanding of what you’re seeing and what I should be looking for.

Cheers

Mine doesn’t attach. Getting AttachParent of the newly spawned actor gives me null.

How are you getting the attached parent? Using the Get Attach Parent node in blueprint will return the SceneComponent that the target is attached to. Attaching an “Is Valid” node shows that if the target has a parent component (is not the root) then the check will be true and it will return that parent. If the target does not have a parent (target is the root component) then the Is Valid check will fail. This was the case in both the MyActorBP as well as the SpawwningBP when setting the root component as the target. In the case of the ChildActorComponent, it returns the default scene component of my SpawningBP (because I never changed this component).

Actually, when in C++, this can cause a real headache if you’re relying on AttachToActor to programatically create an Actor hierarchy. I ended up using the following code to force initialize a root component.

void ARootWidget::PostInitializeComponents()
{
Super::PostInitializeComponents();

if (!RootComponent)
{
	USceneComponent* SceneLayout = NewObject<USceneComponent>(this, TEXT("SceneLayout"));

	SceneLayout->RegisterComponent();
	SceneLayout->OnComponentCreated(); // Might need this line, might not.

	SetRootComponent(SceneLayout);
}

…

1 Like

I have the same issue. Where the BP is getting spawned, and the aactor* returned from the spawn function has a null RootComponent()…

This is crashing in some code, and just breaking other.

The funny thing is, its working fine on other machines in the studio.

For me this turned out to be a corrupt Asset. You really never should get an actor in world that does not have a RootComponent. You should get warnings that one did not exist, and that one has been assigned.

The code above may have fixed the issue. But if the file was corrupt, who is to say how far the corruption went?

If you encounter this issue, you should probably roll back to a working version of the asset, (if you have source control). Or create the asset again from scratch if you dont.