How do I serialize my array in my base using Custom Editor?

Hello,

I’ve built a custom editor script but I can’t save the data upon play. Also note that I do not need to have the data visible in the Inspector, just that it needs to be there upon playing.

[System.Serializable]
class InterfaceTest: MonoBehaviour {

  [SerializeField]
  public IMyInterface[] interfaceArray = new IMyInterface[0];

  void Start(){

     Debug.log(  interfaceArray.Length ); //produces a 0, which is bad

  }

}


using System.LINQ;
using UnityEditor;

[CustomEditor(typeof(InterfaceTest))]
class InterfaceTestEditor: Editor {

  public override void OnInspectorGUI (){

    serializedObject.Update();

     InterfaceTest myTarget = target as InterfaceTest;

     myTarget.interfaceArray = myTarget.gameObject.GetInterfaces<IMyInterface>().Cast<IMyInterface>().ToArray(); //my ext method stuff + LINQ

     Debug.log( myTarget.interfaceArray.Length ); //produces a 1, which is good

     serializedObject.ApplyModifiedProperties();

  }

}

I need to always be able to refresh the InterfaceArray in case I add or remove scripts that implement . In OnInspectorGUI, I receive a value showing that it finds a script and should work…but upon playing the game I don’t have that value stored in myTarget/base script which I need it to have. Any help on this would be greatly appreciated, thank you!

Unity does not play nice with interfaces at all. It would not surprise me to find out that SerializeField doesn’t work with interfaces or collections of interfaces.

Furthermore, if you’re trying to use your IMyInterface objects as SerializedObjects, they will need to inherit from a Unity type like MonoBehaviour or ScriptableObject.

This thread is an excellent read: http://forum.unity3d.com/threads/serialization-best-practices-megapost.155352/