UWorld->SpawnActor() sometimes returns NULL

I have an ACharacter subclass called AGhostCharacter.h with the following member:

 UPROPERTY(EditDefaultsOnly, Category = Gameplay)    TSubclassOf<AEatenGhostEyesPawn> DeathPawn;

It has a method called OnDeath where it spawns another pawn:

UWorld* const World = GetWorld();
    
    if (World && DeathPawn) // Do not enter if there is no world or DeathPawn
    {
        FActorSpawnParameters SpawnParams;
        SpawnParams.Owner = this;
        SpawnParams.Instigator = Instigator;
        
        // spawn the eyes of the ghost after death
        
        const FVector SpawnLocation = GetActorLocation();
        const FRotator SpawnRotation = GetActorRotation();
        
        AEatenGhostEyesPawn* const EyesPawn = World->SpawnActor<AEatenGhostEyesPawn>(DeathPawn, SpawnLocation, SpawnRotation, SpawnParams);

//...

    }

In the above code, UWorld->SpawnActor() only returns an actor some of the time, but returns NULL at other times. This is despite all of the blueprints that inherit from AGhostCharacter.h setting DeathPawn to a valid blueprint that inherits from AGhostEyesPawn.h. I’m quite confused as to what could be causing UWorld-SpawnActor() to randomly return either NULL or a pawn when the class to spawn is defined as a non-NULL value. What could be causing this problem?

I’d suggest putting some breakpoints inside the UWorld::SpawnActor method so you can find out where and why it bails out. An initial guess would be that it could be due to collision - try setting SpawnParams.bNoCollisionFail to true.

If it fails for any reason, there are ample UE_LOG messages that will tell you the reason why it failed. The trick, through, is to actually see the reason before it crashes or hangs (depending if you are using C++ or Blueprints). In any case, make sure your editor’s output log is visible for any messages. If your are using C++, Visual Studio’s Output tab echoes the engine’s output (in a less colorful way, unless you put a visualization plug-in).

All cases are located inside Engine\Source\Runtime\Engine\Private\LevelActor.cpp - line 271+
If it still fails while getting through all those cases, then your case is special and you can report it as a bug.

(I usually get “SpawnActor failed because the given transform ##### is invalid.” because I haven’t check the validity of my spawning point.)

Thanks.
SpawnParams.bNoCollisionFail=true solved the issue.

In 4.22 instead of bool you provide an enum that tells how to handle spawn collision problem. To just make it work you can use

spawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;

which will push spawned object out of the collision space of parent