How To Get Specific Actor By Name?

Hello Everyone,

I have a code piece that tries to find CameraActor in the Level named “Camera_Main”. When I try to log the name of the actor, I receive ID Name. I can not use GetActorLabel because It is only available for Development Builds! I would like to find a specific CameraActor by name. How this can be achieved here?

		TArray<AActor*> CameraActorArray;
		UGameplayStatics::GetAllActorsOfClass(GetWorld(), ACameraActor::StaticClass(), CameraActorArray);

		for (AActor* Actor : CameraActorArray)
		{
			UE_LOG(LogTemp, Warning, TEXT("CameraActor: %s"), *Actor->GetName());

			if(Actor->GetName().Equals("Camera_Main"))
			{
				CameraActor = Cast<ACameraActor>(Actor);
				break;
			}
		}
		
		GetWorld()->GetFirstPlayerController()->SetViewTargetWithBlend(CameraActor, 0);

This is the UE_Log from the console:

LogTemp: Warning: CameraActor: CameraActor_UAID_B025AA4388AE771302_1295454503

Perhaps you can use an actor tag? Add a “Main” tag to your main camera, and then get the actor by tag

Perhaps you can use TSoftObjectPtr as well, if the actor that is looking for the camera is not spawned at runtime

1 Like

The actor you need has a name CameraActor_UAID_B025AA4388AE771302_1295454503 and you need to compare it with him if you want to find this particular actor.

But if this actor spawns during the game - you can get a reference to it after creation, and you don’t need to search for it by name (besides, it could be random).
Or you can save a soft reference to this actor somewhere (for example, in a data asset or data table), if the actor already exists on the level.

1 Like

Thank you for your response!

First I tried to get Camera Actor with ActorHasTag Function. But I always hate it to find something by string-based comparison. Someone can easily change it and no one would notice until we receive errors! Sometimes it would be hard to locate the errors!

Instead, I created a BP_GameController and took my in-level CameraActor reference in there. I do not know if it is the best practice to take references from the Level but I would like to take any advice on how to take references for any Actor included inside the Level.