ChildActor is null after SetChildActorClass?

Good Evening,

everytime I try to call the ChildActor unreal breaks and says that the childActor is null. How is that possible? I have created a ChildActor and I also can see him in the editor spawned.

UChildActorComponent* tempNodeKeaper = CreateDefaultSubobject<UChildActorComponent>(FName(*FString::Printf(TEXT("node%d"), n)));
tempNodeKeaper->SetWorldTransform(FTransform(FVector(-boxSize.X + curPos, -boxSize.Y, 0)));
tempNodeKeaper->AttachTo(this->nodeContainer);

tempNodeKeaper->SetChildActorClass(AKnoten::StaticClass());
tempNodeKeaper->ChildActor->SetActorRelativeScale3D(FVector(size, size, size)); // ChildActor is Null

SetChildActorClass will only create the actor immediately if the component is already registered. Your code is obviously being called from an actor’s constructor, which is too early for the engine to be able to spawn a child actor.

You need to delay the part of your code that accesses the child actor, move it to an override of PostInitializeComponents or BeginPlay.

1 Like

Hi I have similar problem but the thing is that my SetChildActorClass code is in BeginPlay and the ChildActor gets nulled as soon as code exits BeginPlay method any idea why ? I have a RootComponent and my ChildActorComponent is attached to it if I debug the code in begin play I can see the child actor is created but for some reason it gets nulled

ChildActor need to use NewObject to spawn the actor,And it should be in beginplay that it will be better.

1 Like