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: Unity - Scripting API: PrefabUtility, 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:
public static TObject GetCorrespondingObjectFromOriginalSource<TObject> (TObject componentOrGameObject) where TObject : UnityEngine.Object;
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…
They are not the same. Here’s the docs for the two methods:
Due to nested Prefabs and Prefab Variants, there can be a chain of corresponding objects. GetCorrespondingObjectFromSource goes one step along this chain. GetCorrespondingObjectFromOriginalSource goes all the way to the last one.
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.
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.
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.
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.
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
If that doesn’t do what you need, can you go into more details about how it gives different results than you expect?