Search Unity

Difference between "GetCorrespondingObjectFromSource" and "GetCorrespondingObjectFromOriginalSource"

Discussion in 'Scripting' started by Michael-Ryan, Apr 2, 2020.

  1. Michael-Ryan

    Michael-Ryan

    Joined:
    Apr 10, 2009
    Posts:
    184
    Can someone explain the differences between the following two methods and when one should be used over the other?

    PrefabUtility.GetCorrespondingObjectFromOriginalSource(TObject componentOrGameObject)
    Returns the corresponding asset object of source, or null if it can't be found.

    Use this function to get a Prefab Asset object the source was instantiated from. This also returns the corresponding object from the Prefab Asset if the Prefab instance has become disconnected, which can then be used to reconnect the Prefab instance to the Prefab Asset.​

    PrefabUtility.GetCorrespondingObjectFromSource(TObject componentOrGameObject)
    Returns the object of origin for the given object.

    For any object you pass to this function it will follow through the chain of corresponding objects until there are no more and return the last one found.

    This is useful to find the Prefab Asset where the object originated from.​


    I haven't used prefab variants and have no disconnected prefab instances, but in my tests, when passing a prefab instance GameObject to them, they both return the same object: the associated prefab GameObject. When passing a child GameObject of the prefab instance, the object returned is the associated child in the prefab asset.

    Is one method more expensive than the other? From the description, it seems like maybe the "FromOriginalSource" method might be doing more work than the "FromSource" method.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I think you're correct with the theory of prefab variants: one is for if you expect that, the other is if you're unconditionally trying to get to the root perhaps? You'd have to contrive some more examples that use variants and see how the behavior changes, unless someone improves the docs. After you figure it out, you certainly could suggest doc improvements to Unity; they are responsive that way if you make a good case and a simple improvement or request for clarification.