I have a bit of a complex setup where I’m using OnWillSaveAssets to create prefabs of certain objects whenever a scene is saved. I’ll try to give an explanation to the setup, before I describe the actual error.
Basically, a scene might consist of the following objects:
The objects named “GameObject#” can be any given object, which is not connected to a prefab. “PrefabObject#” can be any object which is connected to a prefab. “SpecificObjects” are a holder object for a lot of other objects, which are the objects that are being saved as prefabs in the OnWillSaveAssets method.
The problem then arises when I actually save a scene. Basically everything seems to be working fine, but we’ve discovered that if we remove a component on one of the "PrefabObject"s, apply the prefab and then save the scene, we get the following errors:
CheckConsistency: GameObject does not reference component Animation. Fixing.
com->m_GameObject.GetInstanceID () != 0
When this happens, the component that was removed from the prefab has now been restored on the actual prefab. So basically we’ve lost the changes. The above error message is from a repro where an Animation component was removed, but it happens no matter what component is removed.
I have narrowed it down to that any call to PrefabUtility.CreatePrefab or PrefabUtility.CreateEmptyPrefab will cause this error. If I don’t call these methods inside OnWillSaveAssets, everything works fine. So it seems it is related to the prefab-system somehow, but the weird thing is of course that it is objects that are unrelated to the prefab creation that I’m doing, which is causing the problems.
I hope my explanation has been meaningful enough that someone can figure out what’s going on and can tell me what I’m doing wrong. Is it not allowed to call CreatePrefab inside OnWillSaveAssets maybe?