Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Replacing a prefab without missing references

Discussion in 'Scripting' started by maxxa05, Dec 15, 2017.

  1. maxxa05

    maxxa05

    Joined:
    Nov 17, 2012
    Posts:
    113
    I want to basically mimic the seemingly simple behaviour of drag and dropping a prefab asset on another one. It replaces the target prefab content, without any reference to it going missing in scenes and other assets. I have a bunch of them to modify that way, and I have to do it often. I read other threads on this subject, but none of them truly helped me.

    I first tried to replace the .prefab file directly. After trying a bunch of things like updating the modification time of the file, it finally worked. But then we noticed that some of the references to some of those went missing on other assets and in scenes.

    Then I noticed the PrefabUtility.ReplacePrefab, which seemed to fit the bill nicely. But then I noticed that EVERY reference went missing. I finally noticed that adding ReplacePrefabOptions.ReplaceNameBased did the trick. The problem is, I don't understand well what else it does, and it seems to me that it might have some nasty side effects in some cases. So far I don't see any but I'd like to be sure that I can fully trust this method. If not, do I have any other option?
     
  2. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    384
    I met the same problem with PrefabUtility.ReplacePrefab.
    Is there any solution?
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,658
    Use an editor script to scan the .meta files for the old GUID. Replace it with the new GUID. Job done.
     
unityunity