Accessed None error on a direct refference to a persistent level object after play in editor... sometimes.

I am getting Accessed None errors on Persistent Level reference nodes after using play in editor at least once.

This seems so simple I feel like I need to be overly clear about what I mean.
I have two objects which exist in my level, they aren’t spawned by code. A door and an NPC. I dragged the objects from the list of objects in the level (outliner window) into my level BP to create the reference nodes for them. They have the little “from Persistent Level” text on the node.

LvL-OG-EventGraph

Code using these references will execute flawlessly the first time the code runs after a fresh restart of the editor, every time.

It will sometimes run again without issue.

Other times though (1/2 to 1/3 of subsequent PIE sessions), an Accessed None error will occur on both referenced objects. Once this occurs it will remain in this state for all PIE sessions until the editor is reloaded, or the actor reference node is deleted and replaced. This latter “fix” confuses me though, since I’m literally doing the same action to create what should be an exact replica of the original.

The only special thing I can guess might lead to an issue is that both of these failures are occurring from the Begin Play event for the level, so perhaps there is some problem of the PIE not clearing out the data from the previous PIE session, before starting a new one? Other than that I’m stumped. Any ideas on possible causes or solutions to this kind of issue?

I get actor of class. Then if the cast fails, I delay a fraction of a second and get it again until it casts. Then I set the reference from the cast. If there are multiple doors and I just want 1 by a specific location, then I’d have a master door and then have children doors with tags. So Door1, Door2 etc, and put them in. Then I could “get all actors of class with tag” and grab the door I want and lock the reference in.

Not sure if that’s relevant but it works fine for me.

I mean, I guess it’s possible that could work, but my expectation is that the literal reference to an actor which is guaranteed to always be in the level should be the safest reference possible. I’m not hoping it spawns in time, or hoping it gets found in time, I put it in the level, it should exist before any code is run. Adding a Get Actors node seems to just add another place for the code to fail, possibly further obscuring the actual problem. The problem being, Why is the game acting differently after I have run the PIE once?

This breaks down to two questions:
How do I solve this so that the bad state doesn’t keep screwing up my workflow?

  • Your solution might help

How do I ensure this will never occur in the release build?

  • Your solution would complicate matters and make me less confident in the code… but maybe my confidence is misplaced? Maybe the literal reference is actually less dependable by some eccentricity of the Unreal Engine’s architecture?