- Home /
OnWillSaveAssets causes error in saving prefabs.
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:
GameObject1
GameObject2
SpecificObjects
PrefabObject1
PrefabObject2
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?
Answer by Bunny83 · Sep 17, 2013 at 10:44 AM
Maybe you used Destroy instead of DestroyImmediate ? Can't think of something else without seeing your code.
I know it seems a bit confusing, but the thing is that this error happens on objects I'm not even touching with my script. And I'm not calling Destroy or DestroyImmediate on anything.
I'll try to give an example. Lets say that the game object called "SpecificObjects" in the scene contains three child objects called "c1", "c2", "c3". When I save the scene, I want these three objects to be created as prefabs in a specific folder in my project. All that works fine.
But lets say there's another object in the scene, which isn't a child of "SpecificObjects", called "p1" for instance. "p1" is a prefab object and it has for instance an Animation component attached. If I then try to remove the Animation component, through the inspector, press "Apply" in the inspector and then save the scene, I'll get the error posted in my original post and the Animation component will be restored. So the error is happening on an object that I am in no way manipulating.
Your answer
Follow this Question
Related Questions
Mark gameobject field as changed from prefab 2 Answers
Prefab mode doesn't show prefab, still shows old scene. 7 Answers
Editor-only circular contour around prefab instances 2 Answers
Cant See More Than Two Levels Of Children In The Project Tab 1 Answer
How to make sure editor completed refreshing before running other codes? 1 Answer