I’ve got a generic, serialized class type in Unity that I’m trying to write a CustomEditor for. I’m running into trouble with var myScript = target as <MyClass>
, where it won’t cast it to my (generic) class type:
[Serializable]
public class EnemyStatsAttack {
public float attackDistance = 10f;
//etc
}
[CustomEditor(typeof(EnemyStatsAttack))]
public class EnemyStatsComponentEditor : Editor {
override public void OnInspectorGUI() {
var myScript = target as EnemyStatsAttack; //error: UnityEngine.Object cannot be cast to EnemyStatsAttack
}
}
I’ve tried deriving EnemyStatsAttack from UnityEngine.Object, however then in the inspector the class isn’t serialized and I can’t set the fields (it just shows None (Enemy Stats Attack)
in the box).
How do I fix this?