I have a project that was using Unity 2022.2.1 and I upgraded it to 2022.2.4. Now when I open the project, the Unity Editor constantly scrolls null reference exception errors in the Console. I think they are because some of my materials used a custom shader that I made in Shadergraph that isn’t importing correctly. When I open the shader in Shadergraph, it looks fine. But when I try to save it, I get this error
Asset import failed, "Assets/Shaders/Glow.shadergraph" > InvalidOperationException: Failed to add object of type `UniversalMetadata`. Check that the definition is in a file of the same name and that it compiles properly.
UnityEditor.AssetImporters.AssetImportContext.AddObjectToAsset (System.String identifier, UnityEngine.Object obj) (at <c119aa0913a0473185a8287e39c3f563>:0)
UnityEditor.ShaderGraph.ShaderGraphImporter.OnImportAsset (UnityEditor.AssetImporters.AssetImportContext ctx) (at ./Library/PackageCache/com.unity.shadergraph@14.0.5/Editor/Importers/ShaderGraphImporter.cs:239)
UnityEditor.AssetImporters.ScriptedImporter.GenerateAssetData (UnityEditor.AssetImporters.AssetImportContext ctx) (at <c119aa0913a0473185a8287e39c3f563>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
I get a similar behaviour when trying to save a subgraph asset that went fine in 2022.2.2.
If ever I open this subgraph in 2022.2.4 the saving process throws with this message :
System.InvalidOperationException: Failed to add object of type `SubGraphAsset`. Check that the definition is in a file of the same name and that it compiles properly.
And this callstack :
Void UnityEditor.AssetImporters.ScriptedImporter:GenerateAssetData (AssetImportContext)+0x3 at :-1 C#
Void UnityEditor.AssetDatabase:ImportAsset (String)+0x3 at :-1 C#
Boolean UnityEditor.ShaderGraph.Drawing.MaterialGraphEditWindow:SaveAsset ()+0xd8 at [snip]\PackageCache\com.unity.shadergraph@14.0.5\Editor\Drawing\MaterialGraphEditWindow.cs:[620:21-620:53] C#
Void UnityEditor.ShaderGraph.Drawing.MaterialGraphEditWindow:<set_graphEditorView>b__25_0 ()+0x1 at [snip]\PackageCache\com.unity.shadergraph@14.0.5\Editor\Drawing\MaterialGraphEditWindow.cs:[89:62-89:73] C#
[snip]```
I experienced a similar bug through saving of a shader asset (Failed to add object of type `HDMetadata`) and probably others.
I haven’t been able to reproduce the problem on a new project either. One of the other posts say that his solution was removing/recreating the URP assets so maybe this problem only happens if the URP assets had been originally created by a much older version of Unity (which is the case in my project)? I haven’t tested that fix myself yet.
I had some shaders that were working fine in 2022.2.4, and worked fine after upgrading to 2022.2.7, but after changing the name of my project and reconstructing my Library, I’m now getting this error, too. Weirdly, I can open the shaders in the graph editor just fine.
This also happened to us on our build machine. Every time we did a clean build (with no Library) there were shaders failing. The weird part was that it happened consistently with one machine, only when run by Jenkins (works when run manually from cmd) in batch mode. It used to happen on some other machines but it magically disappeared. After a few days of pulling our hairs, we upgraded the PC in question from Windows 10 to Windows 11 and it got fixed magically.
While debugging we noticed that some script files from the packages are not imported properly, causing the shaders to fail to compile. These files are: Packages/com.unity.shadergraph/Editor/Data/SubGraph/SubGraphAsset.cs Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/UniversalMetadata.cs Packages/com.unity.shadergraph/Editor/Generation/Targets/BuiltIn/Editor/AssetVersion.cs
You can notice this happening in the logs, when those files are imported. When broken it gets different artifact id.
This is how a working version should look:
Start importing Packages/com.unity.shadergraph/Editor/Data/SubGraph/SubGraphAsset.cs using Guid(f28ce979dc22145fd8160e458f72f65d) Importer(-1,00000000000000000000000000000000) -> (artifact id: '725f03f5101e0bbd3d3c2db2e3431786') in 0.001721 seconds
This is how a broken version looks:
Start importing Packages/com.unity.shadergraph/Editor/Data/SubGraph/SubGraphAsset.cs using Guid(f28ce979dc22145fd8160e458f72f65d) Importer(-1,00000000000000000000000000000000) -> (artifact id: '91fbd975371c2af3c0bc8d4231d22c1c') in 0.001852 seconds
Note the produced artifactid 91fbd975371c2af3c0bc8d4231d22c1c. You can confirm this with the “Window/Analysis/Import Activity” window. These ids should be the same for everybody, not just our setup.
I’ve tried reimporting those files before the build starts - it didn’t work as Unity produced the same broken artefact id. Tried embedding the packages, again it didn’t work.
If you open the editor and reimport the scripts by hand, they get fixed. But this is not an option for a build machine.
This didn’t happen with Unity 2021.3.x. It started when we migrated to 2022.3.22f
Cache was disabled during the tests.
Happened with almost empty project as well - it affects the packages, it is unaffacted by the assets.