Search Unity

SaveAsPrefabAsset is doubling all of my scrips on every object?????

Discussion in 'Scripting' started by CloudyVR, Feb 18, 2020.

  1. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    Consider the below code for simplicity. It only instantiats a prefab (to process it) and then replace the same prefab with the instantiated copy:
    Code (csharp):
    1. var prefab = builder.gameObject; //get the gameobject of the scene prefab
    2.  
    3. GameObject real = Instantiate(prefab); //make a copy to use for saving
    4.  
    5. var assetPath = AssetDatabase.GetAssetPath(PrefabUtility.GetPrefabParent(prefab)); //get the path to the prefab
    6.  
    7. PrefabUtility.SaveAsPrefabAssetAndConnect(real, assetPath, InteractionMode.AutomatedAction); //replace prefab with copy
    But now all of my scripts are doubled up on every gameobject in my loaded object!! What have I done wrong?

    My prefab does not show doubled scripts - only the object that was used in the scene has double scripts everywhere - this is the object I need but it becomes all messed up.

    Why does SaveAsPrefabAssetAndConnect cause my scripts to double? How can I stop SaveAsPrefabAssetAndConnect from changing the prefab in the scene and adding double scripts?
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
  3. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    715
    I don't understand what the SaveAsPrefabAssetAndConnect function is doing.

    All I want to do is save changes to a prefab, I don't want it to go on matching stuff and making modifications to my scene objects.

    In the link you provided it states:
    if a single GameObject within the Prefab has more than one of the same Component type attached. In this case you cannot predict which of them will be matched to the existing references.

    What does that even mean?

    And my situation is different, I have only single components on single gameobjects, but once SaveAsPrefabAssetAndConnect runs I then have double components on those same gameobjects.

    I have tried using SaveAsPrefabAsset instead of SaveAsPrefabAssetAndConnect but both exhibit exact same behavior and create multiple (unwanted) components.!!

    Why is this happening?
     
    Last edited: Feb 18, 2020
    xshadowmintx likes this.