Search Unity

Need to save nested prefab instrance as original prefab

Discussion in 'Prefabs' started by Gulliver, Apr 18, 2019.

  1. Gulliver

    Gulliver

    Joined:
    Jan 8, 2013
    Posts:
    101
    Hi all

    I have a situation when I have one prefab nested in the other prefab. And I strongly need to save changes made in the nested prefab to the original prefab. For example
    Prefab A: floor plate (sample)
    Prefab B: spawner of the procedural objects over the host object
    As you can see, prefab B will be used as a nested prefab with many different prefab surfaces: steel floor plates, concrete, wood, walls, or even ceils.
    I want to add prefab B as nested into prefab A, configure it, save, and then -- add it to many other construction prefabs.
    But I can't save nested prefab to original! Any changes made in the nested prefab will remain only in the nested prefab.
    I tried a simple script for calling from the customs inspector when I configure nested prefab.

    public static void save_prefab_ex ( GameObject obj ){
    GameObject prefab = PrefabUtility.GetCorrespondingObjectFromOriginalSource ( obj );
    string prefab_path = AssetDatabase.GetAssetPath ( prefab );
    bool success;
    PrefabUtility.SaveAsPrefabAsset ( obj, prefab_path, out success );
    }

    But it keeps return error:
    ArgumentException: Can't save part of a Prefab instance as a Prefab

    How can I handle it?

    P.S. This code useless as well

    GameObject prefab = PrefabUtility.GetCorrespondingObjectFromOriginalSource ( r.gameObject );
    string prefab_path = AssetDatabase.GetAssetPath ( prefab );
    PrefabUtility.ApplyObjectOverride( r.gameObject, prefab_path, InteractionMode.AutomatedAction );
     
    Last edited: Apr 18, 2019
  2. Gulliver

    Gulliver

    Joined:
    Jan 8, 2013
    Posts:
    101
    Figured out how to do it manually.
    1. Open prefab asset for selected Prefab Instance A in the scene
    2. Select nested prefab B
    3. Override changes made in nested prefab B so they will appear in the original prefab B
    But how to do it with script ?