Download

Save level after add instances of static mesh in code not work

I’m trying to addInstance of static mesh - which works great.
But after I placed it, it’s not being saved in the editor.
The level itself is not save-able and after close it and load again all the instances disappear.

How to add save in code?

TActorIterator<AInstancedFoliageActor> foliageIterator(GEditor->GetEditorWorldContext().World());
AInstancedFoliageActor* foliageActor = *foliageIterator;

FStringAssetReference materialAsset(TEXT("/Game/myModel"));
UObject* MyAsset = materialAsset.TryLoad();
UStaticMesh* staticMesh = (UStaticMesh*)MyAsset;

UInstancedStaticMeshComponent* meshComponent = NewObject<UInstancedStaticMeshComponent>(foliageActor, UInstancedStaticMeshComponent::StaticClass(), NAME_None, RF_Transactional);
meshComponent->AttachTo(foliageActor->GetRootComponent());
meshComponent->SetStaticMesh(staticMesh);
meshComponent->RegisterComponent();

FTransform transform = FTransform();
transform.SetLocation(FVector(100,100,0));

meshComponent->AddInstance(transform);