Trying to add a custom component inside PostProcessModel() in an AssetPostProcessor, but get a missing script.

I can add Unity components just fine (tried LineRenderer and CapsuleCollider), but any of my custom components I try adding don’t work. I feel like I’m missing a step somewhere.

Here’s the code:

// Inside AssetPostProcessorScript.cs in /Assets/Editor/
public class AssetPostProcessorScript : AssetPostprocessor
{   
    void OnPostprocessModel(GameObject go)
    {        
        var comp = go.AddComponent<MyComponent>();        
        comp.someFloat = 0.5f;
    }
}

// Inside MyComponent.cs in /Assets/Scripts/

public class MyComponent : MonoBehaviour
{
    public float someFloat;
}

The postprocessor has no complaint, but when I drag in my .fbx to the scene, the GameObject has a missing script where MyComponent should be.

I don’t (want to) believe Unity doesn’t allow custom components to be added in AssetPostProcessor, so can anyone help me?

(edit) Worth mentioning that I can add MyComponent to the GameObject with no problem manually in the editor.

I guess noone knows? :frowning:

You may want to try using Undo.AddComponent instead. This should mark the gameobject as dirty and should save your changes properly.

If this doesn’t work, try adding EditorUtility.SetDirty:

EditorUtility.SetDirty(comp);

after you added and changed your component. Note that an assetpostprocessor usually has some kind of condition when to execute the action(s). You rarely want / need this on literally every gameobject that you’re importing.