(Case 1193450) PrefabUtility.SaveAsPrefabAsset() with instantiated object breaks lightmap info

Hi. I found that PrefabUtility.SaveAsPrefabAsset() does not work correctly when working with instantiated object.

When you use PrefabUtility.SaveAsPrefabAsset() with GameObject which is instantiated from source GameObject, every call of PrefabUtility.SaveAsPrefabAsset() changes its internal references and structure and it breaks lightmapping information in existing baked scenes.

Here is detailed situation:
Scenes:
SampleSceneA : has RootSourceObject
SampleSceneB : has instance of RootSourcePrefab. And lightmap baked.

Prefabs:
RootSourcePrefab.prefab: generated using SampleSceneA’s RootSourceObject.

If you update RootSourcePrefab by using RootSourceObject, the lightmapping of SampleSceneB is not changed because RootSourcePrefab was replaced with exactly same object. That is correct behaviour.

But if you generate RootSourcePrefab by using the instantiated RootSourceObject and update it like PrefabUtility.SaveAsPrefabAsset(GameObject.Instantiate(RootSourcePrefab)), the lightmapping information of SampleSceneB is lost. This behaviour is incorrect because PrefabUtility.SaveAsPrefabAsset should work as replacement by name policy.

Minimal reproducible project is also attached to Case 1193450.

Thanks.

Edit: I found that references to GameObject is preserved, but references to all of components are lost. (include Renderer, so lightmapping is lost too).

1 Like

I found that exact reproduction step about missing references.

  1. Create prefab without Object.Instantiate().
  2. Instantiate prefab of step 1 into any scene.
  3. Create GameObject and MonoBehaviour which has any public field of GameObject or Component (like Renderer).
  4. Set 3’s reference to the object and components of step 2.
  5. Update 1’s prefab using the same source object, but with Object.Instantiate().
  6. You can see that 3’s some of references are changed to missing.

It seems that references are randomly lost both the GameObject and Component.

The reproduction video is sent to the email of Case 1193450.

2 Likes
1 Like