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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question ArgumentException: UnpackPrefabInstance must be called with a Prefab instance.

Discussion in 'Prefabs' started by vrinaldi, May 20, 2020.

  1. vrinaldi

    vrinaldi

    Joined:
    Mar 9, 2017
    Posts:
    6
    Greetings,
    I'm developing a Monobehaviour script which is supposed to modify the prefab it is attached to.

    In detail, I'm trying to remove all prefab's children containing a Camera component (Editor time only).

    To do so, I supposed to used PrefabUtility methods, in order to:
    1.
    Code (CSharp):
    1. GameObject prefab = PrefabUtility.GetOutermostPrefabInstanceRoot(this.gameObject);
    2.
    Code (CSharp):
    1. PrefabUtility.UnpackPrefabInstance(prefab, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
    After the Unpack I guess I have to PrefabUtility.SaveAsPrefabAsset and PrefabUtility.UnloadPrefabContents, but ArgumentException is triggered when using the UnpackPrefabInstance:

    Code (DebugLog):
    1. ArgumentException: UnpackPrefabInstance must be called with a Prefab instance.
    2. UnityEditor.PrefabUtility.UnpackPrefabInstance (UnityEngine.GameObject instanceRoot, UnityEditor.PrefabUnpackMode unpackMode, UnityEditor.InteractionMode action) (at <17b72532ee2c4da1b6f632d3f1705fe0>:0)
    I don't get where the problem arises, since debugging the code I get correct references and path.

    Thank you to everyone will answer
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,877
    Are you calling this on a Prefab Asset? You can unpack Prefab instances in scenes (and in Prefab Mode). You can't unpack Prefab Assets.
     
  3. vrinaldi

    vrinaldi

    Joined:
    Mar 9, 2017
    Posts:
    6
    Thank you for your answer.
    Yes, I was calling this on a Prefab Asset.
    May I ask what the workflow of altering a Prefab Asset should be?
     
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,877
  5. vrinaldi

    vrinaldi

    Joined:
    Mar 9, 2017
    Posts:
    6
    Thank you, that worked smoothly.