Ok, so I have a component that contains a data class. It looks something like this:
[Serializable]
public DataClass
{
public string someData = "";
public DataClass() {}
}
public class MyDataComponent : MonoBehaviour
{
public DataClass myData = new DataClass();
public void Update() {}
}
Now, I’m trying to add this component in the asset processor as certain models get imported.
void OnPostProcessModel(GameObject go)
{
MyDataComponent foo = go.GetComponent<MyDataComponent>();
if (foo == null)
{
foo = go.AddComponent<MyDataComponent>();
}
// assume we init foo with some data
}
public static void OnPostprocessAllAssets(string[] importedAssets, ...)
{
// assume we get the same asset as above
string modelPath = importedAssets[0];
GameObject model = (GameObject)PrefabUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(modelPath, typeof(GameObject)));
MyDataComponent foo = model.GetComponent<MyDataComponent>();
if (foo == null)
{
// FOO IS ALWAYS NULL
}
}
This seemed to work for me when the contents of DataClass were directly in MyDataComponent, but now that I’ve encapsulated them, suddenly it doesn’t work again.
I can’t figure out why the component returns null now when it was valid before.
Thoughts?