- Home /
PrefabUtility.GetPrefabObject doesn't work!
I am trying to use PrefabUtility.GetPrefabObject to get prefab for scene object. However, when i get object of type "Object" and trying to cast it to game object either
var prefab = PrefabUtility.GetPrefabObject(prefabRoot) as GameObject;
or
var prefab = (GameObject) PrefabUtility.GetPrefabObject(prefabRoot);
I always get null (or InvalidCastException).
Also, when inspecting returned value, it says UnityEngine.Prefab but I've been unable to find this type via Object Browser or IlSpy. I'd really like to get at least name of prefab so I can fetch it myself, but this Prefab object has been useless so far for me.
So far it seems that there is either a bug I didn't get the purpose of this function. Please advise :).
Answer by Bunny83 · Nov 26, 2014 at 02:53 PM
Prefabs are a pure edit time feature. At runtime prefabs don't exist. That's why PrefabUtility.GetPrefabObject (or PrefabUtility in general) is an editor function which can't be used at runtime. The concept of prefabs is a bit complicated since there are different types of prefabs.
At runtime there's no way to tell which object might have been cloned from which. At runtime prefabs are just GameObjects which are loaded, but not part of the scene and therefore they don't get any Update or other callbacks. They purely serve as clone / Instantiate source. You can use Instantiate to clone any object derived from UnityEngine.Object. There's no way, by looking at the clone, to tell from which source an object was created. Even at edit time if you use Instantiate the "connection" to the prefab is lost. You have to use PrefabUtility.InstantiatePrefab when instantiating a prefab in an editor script to keep the connection alive. As said this is only for in-editor usage.
Again, at runtime there are no prefabs. All you get at runtime is a bunch of serialized references to those off-scene objects to be able to instantiate (clone) them into the scene.
This is a good answer, however to completely different problem. Of course I am running this function at editor time. I am not a fool to use UnityEditor dll in runtime since it doesn't event build.
That being said, I rephrase the question: I can't get any valid information that could help me deter$$anonymous$$e the prefab object. I am developing an editor tool and I need to show the user which prefab belongs to which object placed in the scene, that is being showed in editor window. I mean, the "Select" button in Prefab section of Unity inspector has to work somehow right? They don't use the same API or what?
Answer by oakus · Nov 27, 2014 at 03:20 PM
Workaround found with help of Unity staff:
http://forum.unity3d.com/threads/prefabutility-getprefabobject-doesnt-work.282335/#post-1863415
Your answer
Follow this Question
Related Questions
Why do prefabbed meshes go missing whenever I pull an update from Unity Collab? 0 Answers
Change angle of camera in prefab preview 1 Answer
How Mark Prefab Dirty? 1 Answer
Can I force z position to match prefab when it's dragged onto scene? 1 Answer
PrefabUtility.InstantiatePrefab not working with PrefabType.ModelPrefabInstance? 0 Answers