Creating ScriptableObject Asset "Failed to write meta file"

Okay, so I’m attempting to create a level editor that requires creating several assets, one for each campaign, then the maps in the campaign, the missions in each map, then the enemies in those missions. The structure is being saved in /Assets/Resources/Campaigns using editor tools, then the game loads them at run-time and builds the GUI.

The problem only occurred when getting down to the Enemies in the Missions. A new Campaign gets created and stored in Assets/Resources/Campaigns/[campaign-id].asset using AssetDatabase.CreateAsset(), then the same happens for a new Map, in Assets/Resources/Campaigns/[campaign-id]/[map-id].asset, then the same happens again for each new mission in the map, at Assets/Resources/[campaign-id]/[map-id]/[mission-id].asset.

But when I use the same code to create an asset for an enemy in that mission, at Assets/Resources/Campaigns/[campaign-id]/[map-id]/[mission-id]/[enemy-id].asset, CreateAsset throws an error, popping up a window, as below:

If I click ‘Cancel’ a few times, because it will pop up a bunch of times, eventually it will stop showing up, and the console will log the error:

Failed to write meta file 'Assets/Resources/Campaigns/[campaign-id]/[map-id]/[mission-id]/[enemy-id].asset.meta'
UnityEditor.AssetDatabase:CreateAsset(Object, String)
MissionDrawer:OnGUI(Rect, SerializedProperty, GUIContent) (at Assets/Editor/MissionDrawer.cs:175)
UnityEditor.EditorGUILayout:PropertyField(SerializedProperty, GUIContent, Boolean, GUILayoutOption[])
MapDrawer:OnGUI(Rect, SerializedProperty, GUIContent) (at Assets/Editor/MapDrawer.cs:354)
UnityEditor.EditorGUILayout:PropertyField(SerializedProperty, GUIContent, Boolean, GUILayoutOption[])
CampaignDrawer:OnGUI(Rect, SerializedProperty, GUIContent) (at Assets/Editor/CampaignDrawer.cs:158)
UnityEditor.EditorGUILayout:PropertyField(SerializedProperty, GUIContent, Boolean, GUILayoutOption[])
LevelSelectUIManagerEditor:OnInspectorGUI() (at Assets/Editor/LevelSelectUIManagerEditor.cs:285)

I’m using the same methods as the others, and the .asset file gets created. My editors work properly after that, and the values all stay put. It’s just that the .meta file for the asset never gets created, and every time it tries (i.e. whenever importing anything else), it fails to create the .meta and the error window pops up a billion times again.

Any suggestions? The code block that gets executed to create the asset is as follows:

if(GUILayout.Button("Add New"))
	Enemy newEnemy = ScriptableObject.CreateInstance<Enemy>();
	newEnemy.mission = mission;
	string enemyPath = msnPath + "/EN-" + newEnemy.GUID.ToString() + ".asset";

	if(!System.IO.Directory.Exists(msnPath.Replace("/", "\\")))
	  	System.IO.Directory.CreateDirectory(msnPath.Replace("/", "\\"));


	AssetDatabase.CreateAsset(newEnemy, enemyPath);


	serMission = new SerializedObject(mission);


It’s exactly the same as the blocks that create a new Mission, or Map, or Campaign, and those all work without a hitch.

I’ve tried rebooting Unity, rebooting the computer, deleting and re-downloading the project, and finally uninstalling and reinstalling Unity, and nothing works. The computer is a Windows 8.1 laptop.

Thanks in advance.

Okay, so apparently there’s a path-length limit in Windows, and I was pushing it. The .asset files could get created, but that extra 5 characters for the .meta pushed it over the limit.

From the MSDN feedback forums:

[…] 248 characters for the maximum path length and 260 for the maximum filename length.

I reorganized the structure a bit, to shorten the paths, and now it’s functioning properly.