Asset creation issue

Hi all,

I am attempting to use 2 text files to create a bunch of assets, and am doing so successfully except for one major problem. Here is the gist of what I’m doing.

public static void OnPostprocessAllAssets (...)
{
   if (textFilesHaveChanged)
   {
      CreateAssetsFromFile1();
      AssetDatabase.Refresh();
      CreateAssetsFromFile2(); // in the process, loads assets created from file 1
   }
}

The issue is that the assets I create in the first function call are not available when I am creating the second batch of assets. However, if I comment out the first function call, leaving the files that were created the last time the script was run, the second batch of files is able to find them with no issues.

Can anyone lend a hand with this issue? Thanks!!

I cooked up this code to illustrate the exact issue I’m having:

string assetPath = folder + go.name + ".prefab";
		
Object prefab = PrefabUtility.CreateEmptyPrefab(assetPath);
PrefabUtility.ReplacePrefab(go, prefab, ReplacePrefabOptions.ConnectToPrefab);
		
Editor.DestroyImmediate(go);
		
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();

// At this point, a new prefab appears in the project hierarchy. However....

GameObject savedGo = (GameObject)AssetDatabase.LoadAssetAtPath(assetPath, typeof(GameObject));

At the end of the snippet, savedGo is always null for me. If, however, I called the exact same code on a prefab I created in the exact same way, but at a different time (like the last time I had Unity open, for example) it will load the prefab just fine!

To make things weirder, If I instead call AssetDatabase.LoadMainAssetAtPath(assetPath), it seems to return a UnityEngine.Prefab object when I inspect during debugging, but I can’t seem to cast this to a GameObject (nor does it seem like that class even exists!).