Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

PrefabUtility.GetPrefabObject doesn't work!

Discussion in 'Editor & General Support' started by oakus, Nov 26, 2014.

  1. oakus

    oakus

    Joined:
    Dec 27, 2012
    Posts:
    15
    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 :).
     
  2. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    Hi,

    The PrefabUtility API is a little difficult to understand sometimes. GetPrefabObject is only used internally in the editor to check if the object actually exists and is not really useful for editor scripts, like you noticed UnityEngine.Prefab does not even exists.

    The question is, what is you actually want to do? It seems like you already have prefabRoot which I guess it the root game object of the prefab instance?

    Cheers
    Steen
     
  3. oakus

    oakus

    Joined:
    Dec 27, 2012
    Posts:
    15
    Hey, thanks for answer.

    I am trying to write an Editor tool that allows user to choose type (prefab) of unit in my game and than place it into scene (it's instances - in editor time) and sets couple more thing in public vars. It is a convenient utility that removes the neccessity for like 5 click and setting couple vars (which you have to know) and removes proneness to errors.

    But i guess if I introduce a rule not to rename those unit instances and place unit prefabs into Resources, I can determine what is present in scene and allow to change it based on the name.

    Still kinda confusing and would be great to have more description in the documentation.
     
  4. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    Hi,

    I don't understand what it has do with putting prefabs in Resources?

    I understand you are writing a tool that instantiates prefabs, but when you GetPrefabObject() what are you actually expecting to get? Do you have an instance of a prefab asset and want to find the prefab asset?

    I agree the documentation on prefabs need improvement and I know this on the roadmap.
     
  5. Aldrick

    Aldrick

    Joined:
    Feb 19, 2014
    Posts:
    64
    The GetPrefabObject method cannot return a useful Object that I can use to fill the ObjectField or to fetch the actual path from?
     
  6. winxalex

    winxalex

    Joined:
    Jun 29, 2014
    Posts:
    166
    It could be very useful if you answer directly to the question. What
    Its not difficult to understand that Unity need to remove it from documentation or state that they aren't for editor scripts but used by internal Unity editor scripts. Even better is to share how Unity used them internally so we not get stuck by bad documentation and even worst answer by UnityTechnologies.
     
  7. winxalex

    winxalex

    Joined:
    Jun 29, 2014
    Posts:
    166
    The Answer:
    Code (CSharp):
    1. GameObject gameObjectPackedInPrefab=PrefabUtility.GetPrefabParent(gameObjectPrefabInstanceInScene) as GameObject;
    Useful: If you have script which want to "apply" prefab scene instance's(PrefabType.PrefabInstance) changes, to the prefab itself. Save.

    Code (CSharp):
    1. UnityEngine.Object prefabObject = PrefabUtility.GetPrefabObject(gameObject);
    prefabObject is actually of UnityEngine.Prefab type.
    Useful: To find prefab by use of gameObject instance as searchTerm.
     
    IgorAherne likes this.