title: Serialize a class that is derived from a non serializable class
body: <p>I have a problem with serialization, in Unity (using C#).</p>
  
  <p>I have the following classes (the arrows indicate inheritance):</p>
  
  <p><code>Vehicle</code> (abstract) &lt;- <code>Car</code> (abstract) &lt;- <code>Sedan</code></p>
  
  <p>and</p>
  
  <p><code>Vehicle</code> (abstract) &lt;- <code>MotorBike</code></p>
  
  <p>The vehicle class inherits from <code>MonoBehavior</code>.</p>
  
  <p>I have some gameobjects (prefabs), that I attached the classed to. I only use the non abstract classes (i.e. <code>MotorBike</code> and <code>Sedan</code>).</p>
  
  <p><strong>My problem is this:</strong></p>
  
  <p>I want to serialize the data stored within these classes, to be able to save/load. (I use <code>BinaryFormatter</code> for this.) The problem is, <code>MonoBehavior</code> isn't serializable.</p>
  
  <p>All I want is to serialize the data within the vehicle classes, and (upon loading the data) pass that to my Vehicle Factory class, to re-instantiate the prefabs, with the deserialized data. So, I'm not really interested in whatever is stored in the MonoBehavior part.</p>
  
  <p><strong>What I've tried so far:</strong></p>
  
  <p>Using a separate Data class, but this causes all kinds of <a href="https://sourcemaking.com/refactoring/smells/parallel-inheritance-hierarchies" rel="nofollow">problems</a> and I would need to create a child data class for each of my Vehicle classes. And I would need to make the Vehicle class Generic, to indicate which Data class it needs to use, which would require me to use the Generic Type in every place I use the Vehicle type.</p>
  
  <p>Also, I tried to make a SerializationSurrogate for MonoBehaviour, but this didn't work out, because there are numerous other Types that would need a surrogate. And since I'm not interested in the MonoBehaviour, but only in the Vehicle Class and it's child classes, this seemed to be too complex to try.</p>
  
  <p>So, my question is: Does anybody know a way to handle this?</p>