AssetDatabase.SaveAssets() is giving a NullReferenceException

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

private void SavePrefabSetup()
        {
            vehicleBuilderSettings.VehicleName = vehicleName;
            vehicleBuilderSettings.VehicleModel = vehicleModel;
            vehicleBuilderSettings.VehicleType = vehicleType;
            vehicleBuilderSettings.PhysicsMaterial = physicsMaterial;
            vehicleBuilderSettings.BodyName = vehicleBodyName;
            vehicleBuilderSettings.FrontLeftWheelName = frontLeftWheelName;
            vehicleBuilderSettings.FrontRightWheelName = frontRightWheelName;
            vehicleBuilderSettings.BackLeftWheelName = backLeftWheelName;
            vehicleBuilderSettings.BackRightWheelName = backRightWheelName;
            vehicleBuilderSettings.VechicleSettings = vehicleSettings;
            vehicleBuilderSettings.AddEffectsComponent = addEffectsComponent;
            vehicleBuilderSettings.SmokeParticleSystemPrefab = smokeParticleSystemPrefab;
            vehicleBuilderSettings.SmokeCount = smokeCount;
            vehicleBuilderSettings.TrailRendererPrefab = trailRendererPrefab;
            vehicleBuilderSettings.TrailCount = trailCount;
            vehicleBuilderSettings.AddExampleInput = addExampleInput;
            vehicleBuilderSettings.MonoBehaviours = monoBehaviours;

            if (vehicleBuilderSettings != null)
            {
                EditorUtility.SetDirty(vehicleBuilderSettings);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }
        }

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.

1 Like

That’s what I was hoping, just needed confirmation. Thanks!

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?

Yeah, it’s saved - I haven’t noticed if it has ever failed to save.

From the callstack it looks like editor bug in the inspector preview section where models and animations are show.

1 Like

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.

1 Like

I believe that’s because saving an asset triggers asset database update and this triggers inspector update and inspector update triggers preview invalidation, probably

1 Like

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.

1 Like

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 :frowning: )).

  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.

Can confirm this happens on a daily basis here, 2018.4.9

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.

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:

EditorApplication.delayCall += AssetDatabase.SaveAssets;
2 Likes