- Home /
How to know if a gameobjects comes from a certain prefab
Hi all, I have a problem with my custom map editor.
I use an array of prefabs to "paint" (instantiate) things on my scene in editor mode.
I need to know if in the clicked point int the scene there is a gameobject and I also need to know if the gameobject I clicked comes from a prefab in the mentioned array of prefabs.
When I compare the gameobject in scene with the prefabs in the array, I can't figure out how to check if the Gameobject in the scene (wich is a prefab) comes from a non instantiated prefab in the array.
I already tried comparing:
if(go ==arrayGO[i])
if(UnityEditor.PrefabUtility.GetPrefabParent(go) == UnityEditor.PrefabUtility.GetPrefabParent(arrayGO[i]))
if(UnityEditor.PrefabUtility.GetPrefabObject(go) == UnityEditor.PrefabUtility.GetPrefabObject(arrayGO[i]))
if(UnityEditor.PrefabUtility.GetPrefabType(go) == UnityEditor.PrefabUtility.GetPrefabType(arrayGO[i]))
I don know if Im doing it wrong or I am missing something. I could compare them by names, but it's like a ugly solution. Anyone have an idea?
Thanks for reading and all help is welcome.
Answer by Suzuka91 · Oct 15, 2016 at 10:47 AM
Solved after looking on inspector:
It should be like this:
if(UnityEditor.PrefabUtility.GetPrefabParent(go) == arrayGO[i])
{
Debug.Log("go comes from that prefab");
}
Where "go" is an instantiated Gamobject from arrayGO[i], wich is a prefab.
Important note: this only works on edit mode, not at runtime
Answer by Gohloum · Feb 22, 2019 at 04:04 PM
GetPrefabParent() method seems to be deprecated now (Feb 2019). What is the replacement for going about this solution?
I think you want to look at PrefabUtility.GetCorrespondingObjectFromSource()
Your answer
Follow this Question
Related Questions
Check if GameObjects are from the same prefab? 1 Answer
Find specific prefab in a list 2 Answers
How can I tell what prefab a GameObject belongs to? 2 Answers
Spawn Prefabs 1 Answer
Creating an array of prefabs? 4 Answers