Download

Components added in code not persisting?

I have code that dynamically spawns an actor into the current EDITOR world, and adds components to it. This all works correctly. However if you save the level then reload it, all the spawned actors are there, but the components are gone :frowning:


UWorld* world = GEditor->GetEditorWorldContext().World();
AActor* newActor = world->SpawnActor<AActor>(position, rotation);
UHierarchicalInstancedStaticMeshComponent* hism = newObject<UHierarchicalInstancedStaticMeshComponent>(newActor, UHierarchicalInstancedStaticMeshComponent::StaticClass(), *assetName);
hism->RegisterComponent();
hism->AttachTo(newActor->GetRootComponent(), NAME_None, EAttachLocation::SnapToTarget, true);


put your code in the function** virtual void OnConstruction( const FTransform & Transform)**

Remeber to delete the component spawn last time before spawning new one for the same variable

I’m not making a custom Actor so I can’t do that. This is code that fires off in my plugin in response to the user taking an action.

I don’t think I had a valid root component. I called newActor->SetRootComponent(hism) and now it seems to work.