Wait until scripts compile


I’m making an extension for unity that imports some scripts. After I’ve imported the scripts, I need to make a game object and attach to it one of the scripts. Unfortunately, when I try to do that, Unity says that I have to wait until the script has fully compiled. What’s the best way to go here? I mean, I refresh the Asset Database with AssetDatabase.Refresh();

I even load the assets with:
AssetDatabase.ImportAsset(asset, ImportAssetOptions.Default);

But this error still persists. Does AssetDatabase.Refresh have any callback methods or something like that, or do I have to pause the program for some time and then try to attach the script to the gameobject? I tried with System.Threading.Thread.Sleep, but that seems to stop the compiling too.

Any suggestions?

I think the best bet would be EditorApplication.isCompiling. Keep in mind that if Unity recompiles any script, the whole Mono environment is “reloaded” and deserialized. This is also true for editor scripts. Only public variables of serializable types will “survive” the mono restart.