Error when updating script file in AssetModificationProcessor.OnWillCreateAsset

Hi,

I have updated to Unity 6 and I can’t get one of my editor scripts to work as it did in older versions of Unity (for example 2022.3.16f1). I have a Editor script that uses the “AssetModificationProcessor.OnWillCreateAsset” method to intercept when a new script is created and adds some custom code/updates to the script file. When I do this now in Unity 6 I get a loop of error messages,

Message: Build asset version error: assets/scripts/MyCustomScript.cs in SourceAssetDB has modification time of '2025-01-04T08:58:47.0647183Z' while content on disk has modification time of '2025-01-04T08:58:47.0763592Z'

This messages continue until I refresh the assets. This did not happen in older versions then the file was updated and everything seems to work fine. I have tried both with and without using the “AssetDatabase.Refresh();” call.

Here is a simple version of my code that does nothing except for reading the .cs file and writing to it again (e.g. the modification time will be changed on the file),

namespace Assets.Editor
{
	public class AddCustomText : AssetModificationProcessor
	{
		public static void OnWillCreateAsset(string path)
		{
			try
			{
				if (!path.EndsWith(".cs.meta"))
				{
					return;
				}
				path = path.Replace(".meta", "");
				var index = path.LastIndexOf(".");
				if (index < 0)
				{
					return;
				}
				index = Application.dataPath.LastIndexOf("Assets");
				path = Application.dataPath.Substring(0, index) + path;
				if (!File.Exists(path))
				{
					Debug.Log("Unable to update file, file not created");
					return;
				}
				var lines = File.ReadAllLines(path);
				File.WriteAllLines(path, lines);
				AssetDatabase.Refresh();
			}
			catch (Exception ex)
			{
				Debug.LogWarning($"Unable to update custom code in script file: '{ex.Message}'");
			}
		}
	}
}

While searching for a solution I have found suggestions to use “AssetPostprocessor.OnPostprocessAllAssets” insted and tried it but I get the same error then.

Am I missing something here or how can I update the script and still make Unity happy?

/Viktor

It‘s definitely wrong to call Refresh() there. You have to replace it with AssetDatabase.ImportAsset(path) if the modification is supposed to be ‚refreshed‘ immediately. Though it may still only work in OnPostprocessAllAssets because AssetDatabase updates are no longer allowed in all other AssetPostprocessor callbacks.

Be sure to upgrade to the latest Unity 6 patch release as these warnings have been rather common recently and those may be a bug.

Anyhow, such string replace script updates are somewhat of a crutch and by and large can be replaced with alternative solutions. If you absolutely have to modify code look into Roslyn source generators for Unity.

Hi,

Thanks, got it “working” with “AssetPostprocessor.OnPostprocessAllAssets” now without errors or warnings but only if I run the “AssetDatabase.Refresh();” command after the files have been updated, does not fell good or correct, also this is called everytime a script file is updated and not just when it’s created. If I run “AssetDatabase.ImportAsset(path)” I get a recursive loop warning/error from Unity so looks like you should not make that call in that function.

/Viktor