Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pequodification · Nov 16, 2017 at 06:52 PM · unityeditorprefabsprefabutility

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;

 }






Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
  }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pequodification · Nov 17, 2017 at 12:49 PM 0
Share



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"; 
      }
  }


avatar image Grish_tad · Nov 17, 2017 at 12:57 PM 0
Share

ok, then make question answered please

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

73 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to make an existing prefab the variant of another? 0 Answers

Must I always create a public variable for Prefabs? 2 Answers

How can I save an prefab with PlayerPrefs(for character selection) 0 Answers

PrefabUtility.ApplyPrefabInstance throws error when there is Script that does not directly derive on MonoBehaviour 0 Answers

How to add text on avatar material dynamically in unity? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges