Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

AssetDatabase.SaveAssets() is giving a NullReferenceException

Discussion in 'Asset Database' started by Carwashh, Nov 16, 2019.

  1. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    765
    I can't work out why I'm getting this NullReferenceException from AssetDatabase.SaveAssets(). The scriptable object isn't null, otherwise it wouldn't reach the SaveAssets line.

    v2019.2.8f1
    Code (csharp):
    1. private void SavePrefabSetup()
    2.         {
    3.             vehicleBuilderSettings.VehicleName = vehicleName;
    4.             vehicleBuilderSettings.VehicleModel = vehicleModel;
    5.             vehicleBuilderSettings.VehicleType = vehicleType;
    6.             vehicleBuilderSettings.PhysicsMaterial = physicsMaterial;
    7.             vehicleBuilderSettings.BodyName = vehicleBodyName;
    8.             vehicleBuilderSettings.FrontLeftWheelName = frontLeftWheelName;
    9.             vehicleBuilderSettings.FrontRightWheelName = frontRightWheelName;
    10.             vehicleBuilderSettings.BackLeftWheelName = backLeftWheelName;
    11.             vehicleBuilderSettings.BackRightWheelName = backRightWheelName;
    12.             vehicleBuilderSettings.VechicleSettings = vehicleSettings;
    13.             vehicleBuilderSettings.AddEffectsComponent = addEffectsComponent;
    14.             vehicleBuilderSettings.SmokeParticleSystemPrefab = smokeParticleSystemPrefab;
    15.             vehicleBuilderSettings.SmokeCount = smokeCount;
    16.             vehicleBuilderSettings.TrailRendererPrefab = trailRendererPrefab;
    17.             vehicleBuilderSettings.TrailCount = trailCount;
    18.             vehicleBuilderSettings.AddExampleInput = addExampleInput;
    19.             vehicleBuilderSettings.MonoBehaviours = monoBehaviours;
    20.  
    21.             if (vehicleBuilderSettings != null)
    22.             {
    23.                 EditorUtility.SetDirty(vehicleBuilderSettings);
    24.                 AssetDatabase.SaveAssets();
    25.                 AssetDatabase.Refresh();
    26.             }
    27.         }
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,319
    Seems like there's some kind of bug with the internal GameObjectInspector class in 2019.2. It's not anything to do with your code as far as I can tell.
     
    Carwashh likes this.
  3. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    765
    That's what I was hoping, just needed confirmation. Thanks!
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    I've seen this exception completely unrelated to saving Assets. I too think this is some weird Unity bug. Does the asset actually gets saved or not when you see the error?
     
  5. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    765
    Yeah, it's saved - I haven't noticed if it has ever failed to save.
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    From the callstack it looks like editor bug in the inspector preview section where models and animations are show.
     
    Carwashh likes this.
  7. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,319
    Correct. I took a stroll through the Unity Reference Source. A list access is missing a null check or the list isn't getting created early enough or the method is getting incorrectly called after OnDisable. Couldn't see a situation in which that could possibly be caused by the posted code.
     
    Carwashh likes this.
  8. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    I believe that's because saving an asset triggers asset database update and this triggers inspector update and inspector update triggers preview invalidation, probably
     
    Carwashh likes this.
  9. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,319
    I should have been more specific -- I was just reiterating that his code was not doing anything incorrect and the fault lay with Unity's code.

    I'm guessing that you're probably right, though. The SaveAssets call was my thought as well.
     
    Carwashh likes this.
  10. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,947
    I seem to be getting the exact same bug in 2018.3.8

    I only made two changes before it appeared:

    1. Started using Unity's SettingsProvider class (2018.3+ only) - which seems incomplete and unmaintained despite being the new official settings system (c.f. the plethora of threads asking questions about why it doesn't work with packages, etc. c.f. the fact that the official Unity docs give source code that cannot work, by definition! (and the author clearly never tried running :( )).

    2. In order to support the above ... switched code that was loaded one frame AFTER the Editor reloads assemblies (on Editor start, or on re-compile of C#), into running immediately during the Editor's official mechanism for detecting reloads: the class-static-initializer.

    I suspect that item 2 above is the cause. I can remember Unity having serious problems with their undocumented, poorly designed, startup routines going back at least as far as Unity 3.5 :).

    If so ... something to try for anyone else with this problem: re-evaluate what your trigger criteria are for calling SaveAssets(). If it's anything related to Unity itself (e.g. an Editor callback), see if you can insert a 1 frame delay before doing the Save.
     
  11. andersemil

    andersemil

    Joined:
    Feb 2, 2015
    Posts:
    113
    Can confirm this happens on a daily basis here, 2018.4.9
     
  12. unity_72NNT-e0FQsyKw

    unity_72NNT-e0FQsyKw

    Joined:
    Feb 22, 2021
    Posts:
    13
    Not sure this is entirely related but as mentioned above, with the scene display being a part of the error, in my game view I get odd lines that shouldn't exist. Almost like something glitched when previewing the scene in the game view. as I move the game view around or reload my scene, the lines jitter and replace themselves. Both the error and these visual cues came in at the same time for me.

    edit: (UnityEditor.Tilemaps.GridPaintPaletteClipboard:OnDisable() (at C:/Program Files/Unity/Hub/Editor/2020.2.5f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.2d.tilemap/Editor/GridPaintPaletteClipboard.cs:347))

    Possibly something to do with tilemaps in my case.
     
  13. ConnorFailbetter

    ConnorFailbetter

    Joined:
    Jul 2, 2019
    Posts:
    1
    I think this is correct. The workaround is to delay saving the asset until after the inspector has been updated, using EditorApplication.delayCall. For example:
    Code (CSharp):
    1. EditorApplication.delayCall += AssetDatabase.SaveAssets;
     
    longsl and schultzcole like this.