SpawnActor returns null because actor is "pending kill"

I’m trying to implement an “actor transfer” function that saves an actor in a record, then changes the level, then loads the actor in via spawning him by using the record.

When I try to spawn the actor by using “SpawnActor” method, the actor it’s creating is “pending kill”, so it returns null.
Why is that? The function gets called once the new level is loaded and I am trying to spawn an actor, so I don’t get why it would be pending kill.

Can anyone explain why an actor spawned by SpawnActor method would actually be pending kill?

I have set the bNoFail member of SpawnParameters to true, and it gets rid of that problem. However, the actor obviously still is pending kill (and I think gets deleted immediately?). So the actor doesn’t seem to spawn. Howevero, the camera spawns at 0,0,0 of my new level, so apparently the transform doesn’t get applied either? It’s all very confusing.

I will give it a read, thanks.

However, my problem is not that something doesn’t get “deleted” correctly; the actor that is supposed to spawn is determined to be “pending kill” from the get go.
The line I use is the following.

AActor * actor = GetWorld()->SpawnActor<AActor>(record.ActorClass, record.ActorTransform, SpawnParameters);

The first two parameters are valid values from the last level. The third one is a freshly created SpawnParameters object with only the name set (and the bNoFail as an experiment).
This all happens after the level has been loaded, as it gets called by Begin Play of the level blueprint.

If you don’t specify the owner etc. of the SpawnParameters, the persistent level gets used as the outer object (the root set in your words, I guess). It’s really irritating as I don’t see a reason why it should not be able to be spawned.

I found out the reason why. Still thank you to GIGA-Money, as that link is helpful!

The actor does not get spawned because the spawning location was blocked. I intentionally tested a “proper” location, but it seems like the system still didn’t like that one. I now set the Spawn Collision Handling Method to “Always spawn” and it worked properly.

I thought that the bNoFail flag would be responsible to spawn the actor no matter what, turns out it doesn’t.

My log was full because of error messages. When I scrolled up I found the message informing me about the spawn location being blocked.

Well try to understand garbage collection first : see here


Actors and their Components are frequently an exception to this, since the Actors are usually referenced by an Object that links back to the root set, such as the Level to which they belong, and the Actor’s Components are referenced by the Actor itself. Actors can be explicitly marked for destruction by calling their Destroy function, which is the standard way to remove an Actor from an in-progress game. Components can be destroyed explicitly with the DestroyComponent function, but they are usually destroyed when their owning Actor is removed from the game.

IE: something might not be getting deleted correctly. or in the correct order.

Wasted so much time trying to fix it and it was something so simple. Thank You!

Yes I also waste too much time, resove it after changing the “Collision Default” of a blueprint actor.Thanks you for your idea.