Spawn Actor problem- Warning: SpawnActor failed because Class is not an actor class

Hello Everyone. I’ve got stuck with spawning actor. I am getting warning in log LogSpawn: Warning: SpawnActor failed because Class is not an actor class. My code below. IMO it should work cuz Floor is Actor class, I am using TSubclassOf cuz i want have customizable floortype choose.

Header:

UPROPERTY(EditAnywhere)
		TSubclassOf<class AFloor> FloorType;

Cpp:

void ACanYouGameModeBase::SpawnNextFloor(FVector Location)
{
	Location = FVector(1100.f, -400.f, 140.f);
	FRotator Rotation = FRotator(90.f, 0.f, 0.f);
	if (FloorType){
		AFloor* NewFloor = GetWorld()->SpawnActor<AFloor>(FloorType->StaticClass(), Location, Rotation);
		UE_LOG(LogTemp, Warning, TEXT("spawned"));
	}
	
}

I manage to get spawning one actor class with
AFloor* NewFloor = GetWorld()->SpawnActor<AFloor>(FloorType, Location, Rotation); , but i want to do the same in other class and Unreal is crashing, anyone can help?

When you retrieve the “FloorType->StaticClass()” you’re not getting the class value stored in the TSubclassOf, you’re getting the class type of the FloorType variable, which is TSubclassOf and will resolve to UClass (which is not an AActor).

You’ll have to assign the class you want to spawn to your FloorType variable and then use it just as you say it’s working in your comment.

AFloor* NewFloor = GetWorld()->SpawnActor<AFloor>(FloorType, Location, Rotation);

So, if you have an other class you want to use, assign it to FloorType and it should work.
If you need multiple classes, you’ll have to create multiple variables or otherwise assign different classes to the FloorType variable when needed.

1 Like