MyActor->Rename() Crashes During Spawn (Deferred or not)

Hello UE community, I have been facing a spawning issue these past days that I have not been able to resolve. I am working on a UE 4.27 VR project. I need to complete the following objectives:

  1. Spawn actor at specific locations
  2. give each actor a unique name (or get their unique name)
  3. Upon component overlap, get the unique name of each actor.

This is how I call the spawn:

World->SpawnActor<AActor>(ToSpawn, SpawnLocation, rotator, spawnParams); 

I have also tried:

AActor *MySpawn = World->SpawnActor<AActor>(ToSpawn, SpawnLocation, rotator, spawnParams);

Both work.

Issue: Spawning works well, issue comes in when I try and rename the actor (derived from a blueprint class). When I run ToSpawn->Rename(xyz) OR MySpawn->Rename(xyz) (using second spawn line) the engine crashes and gives me the following error:

Rename error during regular spawn (from log file):

[2023.04.25-16.41.01:874][861]LogWindows: Error: Renaming an object (cone_C /Game/LWG_Levels/UEDPIE_0_LWG_A.LWG_A:PersistentLevel.cone_C_1) on top of an existing object (cone_C /Game/LWG_Levels/UEDPIE_0_LWG_A.LWG_A:PersistentLevel.NEWNAME) is not allowed

I tried using deferred spawn and renaming before the spawn was finished but got the same error shown above. The deferred spawn code is below:

AActor* MySpawn = World->SpawnActorDeferred<AActor>(ToSpawn, SpawnTransform);
if (MySpawn == nullptr) {
				GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("MySpawn: Null")));
			}
			else {
				MySpawn->Rename(TEXT("NEWNAME")); // crash
				FString MySpawnNameString = MySpawn->GetName();
				GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, FString::Printf(TEXT("OName: %s"), *MySpawnNameString));
				MySpawn->FinishSpawning(SpawnTransform); // deferred
			}

I am not sure how to go around this issue. The goal is to retrieve the name shown when you hover over an object in the UE Editor.

Note: I am aware of ToSpawn->GetUniqueID() but this isn’t preferred because it generates a new ID every spawn and isn’t always the same. I guess I could use it if nothing else works and then I can attach it to the object for referencing later, but I wanted to see if renaming was possible before I did that.

Thanks in advance!

IDNAme

Spawned actors are already given an unique name by default.

AActor* MySpawn = World->SpawnActor<AActor>(ToSpawn, SpawnLocation, rotator, spawnParams);
UE_LOG(LogTemp, Log, TEXT("Spawned actor name: %s"), *MySpawn->GetName());

If you want to specify name yourself, set it in spawn parameters instead of using Rename :

FActorSpawnParameters SpawnParams;
// Desired name
SpawnParams.Name = "NEWNAME";
// Ask unique name based on desired name
SpawnParams.NameMode = ESpawnActorNameMode::Requested;

AActor* MySpawn = World->SpawnActor<AActor>(ToSpawn, SpawnLocation, rotator, SpawnParams);
UE_LOG(LogTemp, Log, TEXT("Spawned actor name: %s"), *MySpawn->GetName());
// name should be NEWNAME or NEWNAME_# to be unique