[Bug] PrefabUtility.GetCorrespondingObjectFromSource doesn't work in prefab edit mode

Bug report: http://fogbugz.unity3d.com/default.asp?1203667_l6ljh6n0oa212j36
Issue tracker: Not available yet

Repro steps:

  1. Open Test scene in this project.
  2. Select “Main Camera” object in the scene.
  3. See “GetCorrespondingObjectFromSource returns OBJECT” message in console which is CORRECT because it’s an instance of the actual prefab and it can return its source prefab.
  4. Select “Main Camera” prefab in the project.
  5. See “GetCorrespondingObjectFromSource returns NULL” message in console which is CORRECT because we are editing the actual prefab and it doesn’t have a corresponding source. It’s itself already.
  6. Double click “Main Camera” prefab in the project to open it in prefab edit mode.
  7. See “GetCorrespondingObjectFromSource returns NULL” message in console which is WRONG. This time we are editing a live copy of the prefab in a temporary prefab editting scene. To be able to render custom inspector properties, we need GetCorrespondingObjectFromSource to return the real prefab source from the project but it just returns null which prevents us to compare the live values with the prefab values while rendering the inspector.

This is the response from Unity QA team and I agree with them. Thank you.

After investigating, the behavior you have observed is by design. That is because, as documented in Unity - Scripting API: PrefabUtility.GetCorrespondingObjectFromSource, ‘GetCorrespondingObjectFromSource’ is meant to return “Prefab Asset object” that the target object “was instantiated from”. In Prefab edit the objects are directly available and not instances or “live copies” unless they are from nested Prefabs.

To solve your issue, use the object directly available via Inspector parameter ‘target’ and use ‘GetCorrespondingObjectFromSource’ to check if that object is an instance (a clone) of a prefab.
In addition, if there’s a need to detect Prefab edit mode, use ‘PrefabStageUtility.GetCurrentPrefabStage’ function at https://docs.unity3d.com/ScriptReference/Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage.html

1 Like