- Home /
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.
Answer by Bunny83 · May 26, 2018 at 02:22 AM
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.
Thank you! I don't know why, but my issue resolved itself after I loaded up to try this fix (I had tried restarting before, to no avail).
Your info is useful regardless. I do wish I knew what changed / went wrong when I had the problem though.
Your answer
![](https://koobas.hobune.stream/wayback/20220612163227im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Image component vanishes! 0 Answers
Is it possible to create a custom Game Component? 2 Answers
Adding a global custom component to GameObjects 1 Answer
Help removing a Custom Class from a List in UnityScript 1 Answer
2D Animation does not start 1 Answer