GetWorld()->SpawnActor<APawn>(ToSpawn) is returning nullptr

Placement is working perfectly fine with regular actors (the PlaceActor() function works) but for some reason when I try the same function with a Pawn, I get a nullptr. Specifically, even when ToSpawn is NOT a nullptr and is populated with expected values, GetWorld()->SpawnActor<APawn>(ToSpawn) (Inside of PlaceAIPawn()) returns a nullptr. I can’t tell if this is due to updates to the engine or if some unexpected value is being set erroneously.

void ATile::PlaceActors(TSubclassOf<AActor> ToSpawn, int MinSpawn, int MaxSpawn, float Radius, float MinScale, float MaxScale)
{
	TArray<FSpawnPosition> SpawnPositions = RandomSpawnPositions(MinSpawn, MaxSpawn, Radius, MinScale, MaxScale);
	
	for (FSpawnPosition SpawnPosition : SpawnPositions)
	{
		PlaceActor(ToSpawn, SpawnPosition);
	}
	return;
}

void ATile::PlaceAIPawns(TSubclassOf<APawn> ToSpawn, int MinSpawn, int MaxSpawn, float Radius)
{
	TArray<FSpawnPosition> SpawnPositions = RandomSpawnPositions(MinSpawn, MaxSpawn, Radius, 1, 1);
	
	for (FSpawnPosition SpawnPosition : SpawnPositions)
	{
		PlaceAIPawn(ToSpawn, SpawnPosition);
	}
	return;
}

void ATile::PlaceActor(TSubclassOf<AActor> ToSpawn, const FSpawnPosition SpawnPosition)
{
	AActor* Spawned = GetWorld()->SpawnActor<AActor>(ToSpawn);
	Spawned->SetActorRelativeLocation(SpawnPosition.Location);
	Spawned->AttachToActor(this, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));
	Spawned->SetActorRotation(FRotator(0, SpawnPosition.Rotation, 0));
	Spawned->SetActorScale3D(FVector(SpawnPosition.Scale));
}

void ATile::PlaceAIPawn(TSubclassOf< APawn> ToSpawn, const FSpawnPosition SpawnPosition)
{
	
	APawn* Spawned = GetWorld()->SpawnActor< APawn>(ToSpawn); // Problem is here
	Spawned->SetActorRelativeLocation(SpawnPosition.Location);
	Spawned->AttachToActor(this, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));
	Spawned->SetActorRotation(FRotator(0, SpawnPosition.Rotation, 0));
	Spawned->SpawnDefaultController();
	Spawned->Tags.Add(FName("Enemy"));
}

Turns out the pawn was trying to spawn inside the ground, which caused spawnactor to fail.
(Full warning: SpawnActor failed because of collision at the spawn location [X=0.000 Y=0.000 Z=0.000])

I managed to use the following code to get it working, which both forces it to spawn and spawns it above the ground (the pawns were previously inside the ground):

FVector Location(0.0f, 0.0f, 89.f); // Drags pawn above the floor 
FRotator Rotation(0.0f, 0.0f, 0.0f);     
FActorSpawnParameters SpawnInfo; 
SpawnInfo.SpawnCollisionHandlingOverride = SpawnActorCollisionHandlingMethod::AlwaysSpawn; // Forces the pawn to spawn even if colliding
APawn* Spawned = GetWorld()->SpawnActor<APawn>(ToSpawn, Location, Rotation, SpawnInfo); // Use of SpawnInfo and offset vectors

Privacy & Terms