Hello all, I’m trying to generate code with Editor scripts to create MonoBehaviour classes. I’m using an AssetPostprocessor class to get a callback when the MonoScript file is imported. From within that callback I’d like to create a GameObject in my scene and add on it the MonoBehaviour in the imported MonoScript. Now it seems to work up to the point where I get the MonoScript asset with AssetDatabase.LoadAssetAtPath. When I call monoScript.GetClass(), it returns null. I’m guessing the MonoScript file is found but the actual type contained in it isn’t “loaded” yet?
Is it possible to do what I’m trying to do? Here’s my code when I get the callback from AssetPostprocessor :
AssetDatabase.ImportAsset( _assetPath );
MonoScript script = AssetDatabase.LoadAssetAtPath<MonoScript>( _assetPath );
if (script != null)
{
GameObject sceneGO = new GameObject( );
//this line debugs "AddComponent asking for invalid type" because script.GetClass() returns null
sceneGO.AddComponent( script.GetClass() );
}