C++ - Dynamically created components are disappearing

void ABuildPreviewActor::CreateBoxComponents()
{
    UE_LOG(LogTemp, Warning, TEXT("16. CreateBoxComponents called"));
    // Yeni veya var olan komponentleri kontrol et ve oluştur
    for (FBoxComponentInfo& Info : BoxComponents)
    {
        if (!Info.BoxComponent)
        {
            UE_LOG(LogTemp, Warning, TEXT("17. Creating new BoxComponent"));
            Info.BoxComponent = NewObject<UBoxComponent>(this, UBoxComponent::StaticClass(), NAME_None, RF_Transactional);
            Info.BoxComponent->CreationMethod = EComponentCreationMethod::Instance;
            Info.BoxComponent->SetHiddenInGame(false);
            Info.BoxComponent->SetMobility(EComponentMobility::Movable);
            Info.BoxComponent->RegisterComponent();
        }
        else if (Info.BoxComponent->GetAttachParent() != RootComponent)
        {
            UE_LOG(LogTemp, Warning, TEXT("18. Attaching existing BoxComponent to RootComponent"));
            Info.BoxComponent->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
        }
    }
    UpdateComponentRenderStates(); // 19. Bileşen render durumlarını güncelle
}

Two demo boxes

You can move the boxes after first compile after package is marked a dirty. Should probably just update the component in some way.

Though cleanup doesn’t seem to work after removing the boxes. They need to be deleted by hand as they are created with the new command.

Two instances in level after restart of editor

loaded
Boxes are intact and positions load.

1 Like