Failed to unpersist: MonoBehaviour ID and then unity destroys our scene.

On random occasions, when going to and from playmode, everything in our scene gets deleted. You end up with just a blank scene.

We also get a lot of these when this happens.

Failed to unpersist: MonoBehaviour ID: -406172 FileID: 1298162478

(Filename: C:/buildslave/unity/build/Runtime/Misc/SaveAndLoadHelper.cpp Line: 631)

Failed to unpersist: Transform ID: -406170 FileID: 1298162477

(Filename: C:/buildslave/unity/build/Runtime/Misc/SaveAndLoadHelper.cpp Line: 631)

We’re on Unity 5.6.5f1

Is there a way to fix this issue or do a workaround?
Or better, does anyone know why this happens?

Ok - This problem has been plaguing me for a few years now and a few days ago I decided enough’s enough and today I finally found a solution. I’m putting it here in hopes it will be of help to someone.

I believe there is a bug in Unity’s internal scene code somewhere , probably scene or prefab serialization (I don’t think it’s a corrupt file). In my case the root of the problem was my own pool manager.

For me It seems specifically it was caused by using an ‘Instantiate’ command inside an Awake function suggesting that the scene is not ready for Instantiation at that point. When the prefab was added to the scene it seems it invalidates the prefab and then destroys it. When the prefab becomes invalidated it also invalidates the parent game objects all the way up to the root of the scene and since I have a habit of putting all my game objects into a single parent game object on the root, this made it seem like the whole scene was getting deleted (which is was).

This would happen either on pressing play or on pressing stop.

When I moved my Pool Manager out of its parent and into the root of the scene I noticed it was only the Pool Manager and it’s associated game objects that were getting deleted, because I was putting the Instantiated prefabs inside them.

Long story short after moving the Instantiate function to a later stage in the game cycle I’ve completely eradicated the problem.

For extra context (I’m working on the project that has the problem): It only happens when you exit playmode and go back to the editor. It happens, seriously, seemingly at random. Saw that some person had a problem with selecting things then playing and it showed up so we’ve tried everything and every combination of selecting things in the editor then stopping, then deselecting things, stopping, pausing stopping etc etc etc we have no clue what causes it but it’s nothing straight up obvious. Really tricky to debug since we can’t recreate it, not until it destroys the scene and we loose our work(!)

It seems to affect certain scenes so we also suspect that maybe the scene file is corrupt in such a way that it works 98% of the time and immolates itself somehow for the other 2% but we have no hard proof of this being the case either

We have a custom hierarchy drawer but we get no errors from it at all. it’s pretty odd so even if you have some of your own experiences that would be helpful ^ _ ^

(note: we’ve made a scene backup on play system as a workaround so our data is ultimately safe but we would prefer to figure it out properly and avoid having to restore from backups if possible :slight_smile:

I’ve noticed this as well. It only happens when I lock my inspector on an item, oddly enough
EDIT: On 2018.3