Search Unity

DIfference between GetCorrespondingObjectFromOriginalSource and GetCorrespondingObjectFromSource

Discussion in 'Prefabs' started by Huxii, Jun 7, 2020.

  1. Huxii

    Huxii

    Joined:
    Sep 15, 2018
    Posts:
    37
    Hello,
    I am trying to find a method, with which I could find out, if a Game object is / is created from a prefab. If the GameObject has a parent, I am not intressted, if the parent is a prefab, but the Child itself. I tried probably every possible method from here: https://docs.unity3d.com/ScriptReference/PrefabUtility.html, but unfortunatly nothing works. So I wanted to know, how exactly the methods are described. I got a look on the two most probable ones: GetCorrespondingObjectFromOriginalSource and GetCorrespondingObjectFromSource and found out, that they have the same definition:

    Code (CSharp):
    1. public static TObject GetCorrespondingObjectFromOriginalSource<TObject>  (TObject componentOrGameObject) where TObject : UnityEngine.Object;
    2.  
    3. public static TObject GetCorrespondingObjectFromSource<TObject>   (TObject componentOrGameObject) where TObject : UnityEngine.Object;

    This leads me to the question, why the two exist if they are the same...

    Does anybody know that?

    Thank you very much
    Huxi
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
  3. Huxii

    Huxii

    Joined:
    Sep 15, 2018
    Posts:
    37
    Hello @runevision
    I have read the documentation, but I still don't understand the exact meaning. But still, it looks, that these two are not what I am looking for. Is there a method, with which help I can find out, whether a children has/is a prefab? I guess I tried already every method from the PrefabUtilities, but it seams, that every method searches after a prefab just in the parent, but not for the child itself.
     
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Perhaps this session can help get a deeper understanding:


    I'm not completely sure what you mean.

    You can always get a parent by getting transform.parent and then (if it's not null) perform checks on that. This way you could check if the parent is a Prefab instance using
    https://docs.unity3d.com/ScriptReference/PrefabUtility.IsPartOfPrefabInstance.html
    This will return true if the parent is part of a Prefab instance and false if it isn't.

    If you want to check if a given GameObject is the root of a Prefab instance, then you can use of of these:
    https://docs.unity3d.com/ScriptReference/PrefabUtility.IsAnyPrefabInstanceRoot.html
    https://docs.unity3d.com/ScriptReference/PrefabUtility.IsOutermostPrefabInstanceRoot.html

    This will return true for objects that are the root of a Prefab instance, but not for the children inside the Prefab instance - except if a child is the root of a different Prefab instance. IsOutermostPrefabInstanceRoot will not return true for nested Prefabs while IsAnyPrefabInstanceRoot will.
     
    Wappenull likes this.
  5. Huxii

    Huxii

    Joined:
    Sep 15, 2018
    Posts:
    37
    I try it with an example. You have this GameObject:

    Door
    - Frame
    - Wing

    and you want to know if the "Frame" itself is a prefab (or was created from a prefab). So, I am not interested to know, if the Door as parent is a prefab, but The frame itself.
    Unfortunately, all methods I tried are checking just this; They go to the Frame and then the hierarchy up, to see if it's parent is an prefab. But well, that's not what I want.
     
  6. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Prefabs can contain multiple GameObjects, but always only one root GameObject. When you say you want to check if Frame itself is a Prefab, not the parent, it sounds like you want to know if Frame is the root of a Prefab. You can do this with
    https://docs.unity3d.com/ScriptReference/PrefabUtility.IsAnyPrefabInstanceRoot.html

    If that doesn't do what you need, can you go into more details about how it gives different results than you expect?
     
  7. Huxii

    Huxii

    Joined:
    Sep 15, 2018
    Posts:
    37
    Hi, it actually works :).
    I didn't consider this because the name was not convincing me that it would do what I needed.

    Thank you very much
    Huxi