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 eduwushu · Oct 04, 2011 at 08:12 AM · prefabupdatemodel

Replacing a prefab with a new one to update model changes

Hi all! I've been trying to solve a problem we have with prefabs for some days without success so I feel it's time to ask the community for ideas. We have the situation where we have several objects on a scene instantiated from a prefab that was created using a model asset from the repository. On this prefab several components were added. Later the model has to be changed and several additional bones were added to it. This brokes down the mesh on the game. So we must update the prefab to get the changes on the model. Dragging and dropping the model asset again into the prefab works but it makes all the components the prefab has to be lost. To avoid this I have come to a semi automated solution using scripting. Here's the code:

         GameObject tempInstance = GameObject.Instantiate(model) as GameObject;

         GameObject newInstance = EditorUtility.InstantiatePrefab(oldPrefab) as GameObject;

         Component[] fromComps = newInstance.GetComponents(typeof(Component));

         foreach(Component c in fromComps)

         {

             Component nc = tempInstance.GetComponent(c.GetType());

             if( nc == null)

                 nc = tempInstance.AddComponent(c.GetType());

             foreach (FieldInfo f in c.GetType().GetFields())

             {

               f.SetValue(nc, f.GetValue(c));

             }

             foreach (PropertyInfo p in c.GetType().GetProperties())

             {

                 if(p.CanWrite && p.CanRead)

                     p.SetValue(nc, p.GetValue(c,null),null);

             }

         }

         EditorUtility.ReplacePrefab(tempInstance,oldPrefab);

         AssetDatabase.Refresh();
 
         GameObject.DestroyImmediate(tempInstance);

         GameObject.DestroyImmediate(newInstance);
 

Where 'model' is a reference to the source model asset in the library and 'oldPrefab' is a reference to the prefab object that we want to update.

The problem is that when I call 'ReplacePrefab' it seems that all the instances that were created from the old prefab are deleted and created again, so all the references that I had to these objects in other objects of the scene are lost. In fact, the call to DestroyImmediate(newInstance) gives an error that I'm trying to access an object that has already been destroyed. I don't know how to avoid all the instances to be 'recreated' when updating the prefab, neither I know if this is possible.

Other solution would be to create the prefabs not using the model asset directly, but using an empty game object and then adding the model as a child of it. But this solution would mean that I must change a lot of script code for the components (they must acces the model or the animation component one level deeper in the hierarchy) and also to make again all the scenes, which is a lot of work at this point of development.

¿Any help?

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

Answer by nsxdavid · Nov 29, 2011 at 12:18 AM

There is a plugin in the Asset Store called Merlin's Prefab Lab which more-or-less solves these problems. Updates to models are reflected on the prefabs in another folder without loss of components, etc. The only thing it doesn't seem to handle is if that prefab is nested in yet another prefab. But you get the source code, so you might be able to extend it especially given how much of the heavy lifting (and R&D) it's done for you by that point. Not expensive, worth ever penny.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Changing prefab doesn't update instances (in scene aswell as in other prefabs).. 3 Answers

Why Cant I see the Prefab : Select, Revert and Apply options in the Inspector window 1 Answer

assets hidden children of an fbx. 2 Answers

Update function in a UnityScript attached to 50 clones of a prefab: heavy or not? 1 Answer

Keep constant number of prefabs 1 Answer


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