- Home /
 
PrefabUtility.CreatePrefab fails if prefab already exists
I am trying to automatically create, then update a prefab which stores metadata about my scenes. This all happens in edit mode and an update is triggered whenever something changes in the scene.
My current method works when the prefab is empty, or doesn't exist, however the PrefabUtility.CreatePrefab method fails with the following errors if the prefab already exists:
 Assertion failed on expression: 'm_InstanceID != InstanceID_None'
 UnityEditor.PrefabUtility:CreatePrefab(String, GameObject, ReplacePrefabOptions)
 PrefabUpdater:refreshMetaData() (at Assets/PrefabUpdater.cs:31)
 PrefabUpdater:Update() (at Assets/PrefabUpdater.cs:11)
 
 [/Users/builduser/buildslave/unity/build/Runtime/BaseClasses/BaseObject.h line 392] 
 (Filename: Assets/PrefabUpdater.cs Line: 31)
 
 Prefab not saved due to OnWillSaveAssets callback.
 UnityEditor.PrefabUtility:CreatePrefab(String, GameObject, ReplacePrefabOptions)
 PrefabUpdater:refreshMetaData() (at Assets/PrefabUpdater.cs:31)
 PrefabUpdater:Update() (at Assets/PrefabUpdater.cs:11)
 
 [/Users/builduser/buildslave/unity/build/Editor/Src/Prefabs/Prefab.cpp line 251] 
 (Filename: Assets/PrefabUpdater.cs Line: 31)
 
               I've included some example code below, which I'm running in 5.6.3p4 on a Mac. If anyone has any suggestions how I could fix this (or a better way to go about it!) it would be greatly appreciated!
 using UnityEngine;
 using UnityEditor;
     
 [ExecuteInEditMode] 
 public class PrefabUpdater : MonoBehaviour {
     
         private string path = "Assets/";
         private string filename = "metadata";
     
         void Update () {
             refreshMetaData();
         }
     
         private void refreshMetaData () {
             string fullPath = getPrefabFullPath();
             //Try to load existing prefab 
             GameObject go = AssetDatabase.LoadAssetAtPath(fullPath, typeof(GameObject)) as GameObject; 
             //if not found create a new gameobject 
             if (go == null) {
                 go = new GameObject(filename);
             }
             //Update a counter 
             Counter counter;
             if (go.GetComponent<Counter>() == null) {
                 counter = go.AddComponent<Counter>();
             }
             else {
                 counter = go.GetComponent<Counter>();
             }
             counter.Count += 1;
             //save the prefab
             PrefabUtility.CreatePrefab(fullPath, go, ReplacePrefabOptions.ReplaceNameBased);
             DestroyImmediate(go);
         }    
         private string getPrefabFullPath () {
                     return path + filename+ ".prefab"; 
         }
 }
     
 public class Counter : MonoBehaviour {
         
             public int Count;
 }
 
              Answer by Grish_tad · Nov 17, 2017 at 10:37 AM
I think it will work for you
 using UnityEngine;
  using UnityEditor;
      
  [ExecuteInEditMode] 
  public class PrefabUpdater : MonoBehaviour {
      
          private string path = "Assets/";
          private string filename = "metadata";
          private bool isGameObjectExist;
          void Update () {
              refreshMetaData();
          }
      
          private void refreshMetaData () {
              string fullPath = getPrefabFullPath();
              //Try to load existing prefab 
              GameObject go = AssetDatabase.LoadAssetAtPath(fullPath, typeof(GameObject)) as GameObject; 
              //if not found create a new gameobject 
              if (go == null) {
                  isGameObjectExist= false;
                  go = new GameObject(filename);
              }
 else
 isGameObjectExist= true;
              //Update a counter 
              Counter counter;
              if (go.GetComponent<Counter>() == null) {
                  counter = go.AddComponent<Counter>();
              }
              else {
                  counter = go.GetComponent<Counter>();
              }
              counter.Count += 1;
              //save the prefab
 if(isGameObjectExist)
 {
              PrefabUtility.ReplacePrefab( go, prefab , ReplacePrefabOptions.ReplaceNameBased);
 }
 else  UnityEngine.Object prefab = PrefabUtility.CreatePrefab(fullPath, go, ReplacePrefabOptions.ReplaceNameBased);
              DestroyImmediate(go);
          }    
          private string getPrefabFullPath () {
                      return path + filename+ ".prefab"; 
          }
  }
      
  public class Counter : MonoBehaviour {
          
              public int Count;
  }
 
              
 After a few tweaks, that did the trick. Thanks for taking the time. 
 Here's what worked for me in case anyone else runs into this problem :
  using UnityEngine;
  using UnityEditor;
  
  [ExecuteInEdit$$anonymous$$ode] 
  public class PrefabUpdater : $$anonymous$$onoBehaviour {
  
      private string path = "Assets/";
      private string filename = "metadata";
      private bool fileExists;
  
      void Update () {
          refresh$$anonymous$$etaData();
      }
  
      private void refresh$$anonymous$$etaData () {
          string fullPath = getPrefabFullPath();
          //Try to load existing prefab 
          Object obj = AssetDatabase.LoadAssetAtPath(fullPath, typeof(GameObject)) as GameObject; 
          GameObject go;
          //if not found create a new gameobject 
          if (obj == null) {
              go = new GameObject(filename);
              fileExists = false;
          }
          else {
              go = PrefabUtility.InstantiatePrefab(obj) as GameObject;
              fileExists = true;
          }    
          //Update a counter 
          Counter counter;
          if (go.GetComponent<Counter>() == null) {
              counter = go.AddComponent<Counter>();
          }
          else {
              counter = go.GetComponent<Counter>();
          }
          counter.count += 1;
          //save the prefab
          if (fileExists) {
              PrefabUtility.ReplacePrefab(go, obj, ReplacePrefabOptions.ReplaceNameBased);
          }
          else {
              PrefabUtility.CreatePrefab(fullPath, go, ReplacePrefabOptions.ReplaceNameBased);
          }
          DestroyImmediate(go);
      }    
  
      //assemble prefab path
      private string getPrefabFullPath () {
          return path + filename+ ".prefab"; 
      }
  }
                 Your answer