Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instances of old prefabs not having fields set that the prefab itself has (as seen in prefab mode).

Discussion in 'Prefabs' started by JeffersonTD, Dec 16, 2018.

  1. JeffersonTD

    JeffersonTD

    Joined:
    Feb 5, 2013
    Posts:
    268
    I updated my personal project from 2018.1 to 2018.3 (pretty much just to get those long-waited nested prefabs :)), but encountered a weird issue with it: instances of most prefabs had their references set to null.

    I investigated the issue for a while and noticed that even though right after instantiation the prefab instances don't seemingly have any overrides compared to the prefab, many of the fields are just empty, even though when opening the corresponding prefab, the references are alright. Well, ultimately I found that I can fix the issue by opening the prefab, doing some kind of a change so that it allows for me to save it, and then save it (maybe autosave works also).

    I also noticed that just doing the save isn't enough if a field has "HideInInspector" on it. There the solution seems to be to remove the HideInInspector, then do the save, and then put the HideInInspector back. Can this be a more general bug with the new prefab work flow?

    So now it seems that I have to go through all my prefabs to manually fix them by simply doing and undoing a change and then saving the prefabs. If there is a migration needed for the old prefabs, why isn't it automatic?
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Prefabs should automatically reimport after you've fixed all compile issues. If they for some reason don't you can manually reimport the Prefabs in question, and that should fix it.
     
    JeffersonTD likes this.
  3. CapnCromulent

    CapnCromulent

    Joined:
    Sep 7, 2010
    Posts:
    45
    I've had the same problem since we first upgraded to 2018.3.b9, and it's still present in f2. Since we have hundreds of prefabs that need to be touched, here's s small script that seems to handle most of our cases:

    Code (CSharp):
    1. private void UpdatePrefabs()
    2.         {
    3.             int successCount = 0;
    4.             string[] guids = AssetDatabase.FindAssets("t:prefab");
    5.             foreach (string guid in guids)
    6.             {
    7.                 string assetPath = AssetDatabase.GUIDToAssetPath(guid);
    8.                 GameObject prefab = PrefabUtility.LoadPrefabContents(assetPath);
    9.                 EditorUtility.SetDirty(prefab);
    10.                 bool success;
    11.                 PrefabUtility.SaveAsPrefabAsset(prefab, assetPath, out success);
    12.                 if (success)
    13.                 {
    14.                     ++successCount;
    15.                 }
    16.                 {
    17.                     Debug.LogWarning($"Error saving {prefab.name} at {assetPath}");
    18.                 }
    19.                 PrefabUtility.UnloadPrefabContents(prefab);
    20.             }
    21.  
    22.             Debug.Log($"Processed {successCount}/{guids.Length} prefabs");
    23.         }
     
    JeffersonTD likes this.
  4. JeffersonTD

    JeffersonTD

    Joined:
    Feb 5, 2013
    Posts:
    268
    Thanks! I didn't know there even was a "reimport" option, but apparently that does indeed fix them.

    And thanks for sharing the code CapnCromulent. For my case I only have prefabs in a few folders, so I can pretty easily just multi-select them and use the reimport.

    There is still at least one issue that emerged after updating Unity, but I'll try to check that at some point (and that's actually a place where nested prefabs will be nicely useful actually :) ). The TextMesh Pro also got broken by the update, but for my small project it doesn't matter much.