I have a Unity editor script that creates another script called TestComponent “on the fly”. So this editor script that I have actually creates TestComponent as a script and a prefab in the assets folder, and then attempts to add TestComponent to the prefab. The problem is that script creates TestComponent using the .net File.WriteAllText and unity editor needs to refresh the asset database before it could recognise the TestComponent.
So, I call AssetDatabase.Refresh() to refresh the assets. However, when I do so, I get the following error:
Script TestComponent has not finished compilation yet. Please wait until compilation of the script has finished and try again.
UnityEngine.GameObject:AddComponent(String)
And I still cannot add the component to the prefab. It seems like the asset refresh is asynchronous and I have to wait for the refresh to complete before I could add the component. Since the lines of the code in my editor script just follows one after another, I get that error.
Then now, how can I make sure that the editor has completed refreshing the assets and is ready to run the other lines that will add the component script to the prefab?