UChildActorComponent::GetChildActor() does not return Actor

Hi,

i am trying to add some UChildActorCompoent’s to an custom AActorA in c++. Afterwards i want to iterate all UChildActorCompoent’s and calculate some value based on the AActorB’s.

My AActorA class has a property

TSubclassOf<AActorB> bClass;

in the AActortA::PostEditChangeProperty(struct FPropertyChangedEvent& e) function i create a UChildActorCompoent if the value of bClass changes.

void AActorA::PostEditChangeProperty(struct FPropertyChangedEvent& e) {
        ...
        UChildActorComponent* comp = NewObject<UChildActorComponent>(this);
        comp->SetupAttachment(RootComponent);
        comp->SetChildActorClass(bClass.GetDefaultObject()->GetClass());
        comp->CreateChildActor();
        comp->RegisterComponent();
        
        AActorB* b = Cast<AActorB>(comp->GetChildActor());
        if (b) {
          UE_LOG(LogTemp, Warning, TEXT("found actor"));
        }
        ...
}

This code perfecly works and the log shows “found actor”. (However, the ChildActorComponent is at first not visible in the Editor. It becomes visible by reselecting the AActorA in the editor)

If i try to get all the ChildActorComponent’s and iterate them, i can’t receive the AActorB’s.

void AActorA::doSomething() {

	TSet<UActorComponent*> comps = GetComponents();

	int i = 0;
	for (UActorComponent* comp : comps) {
		UE_LOG(LogTemp, Warning, TEXT("Component: %d"), i);
		UChildActorComponent* childC = Cast<UChildActorComponent>(comp);
		if (childC) {		
			AActorB* b= Cast<AActorB>(childC->GetChildActor());
			if (b) {
				UE_LOG(LogTemp, Warning, TEXT("ActorB: %s"), *b->GetName());
			}
		}
		i++;
	}
}

If i call this function all components of the AActorA are iterated but i don’t get the AActorB’s.

I would be very grateful if someone has a solution to this problem and the update problem of the editor.