title: Override base class property to a derived type and access via base class
body: <p>imagine the following classes:</p>
  
  <pre><code>public abstract class State {}
  
  public abstract class View : MonoBehaviour {
      public State state;
  }
  </code></pre>
  
  <p>Now I want to derive from both classes to create <code>StateA</code> and <code>ItemA</code>.</p>
  
  <pre><code>public class StateA { // some properties }
  
  public class ViewA : View {
      public StateA state;
  }
  </code></pre>
  
  <p><code>ViewA</code> should only accept <code>StateA</code> as its state. Setting a hypothetical <code>StateB</code> should not be possible. When I am trying to set the state of <code>ViewA</code></p>
  
  <pre><code>View view = someGameObject.GetComponent&lt;View&gt;;
  view.state = new StateA ();
  </code></pre>
  
  <p>only the state of the base class will be set. Casting to <code>ViewA</code> is not really possible in my situation because it would have to be cast dynamically.</p>
  
  <p>Now I came up with two possible solutions.</p>
  
  <p><strong>Solution 1 - Type check and cast</strong></p>
  
  <p>All state classes remain unchanged. The base class is checking for the correct type of the state.</p>
  
  <pre><code>public abstract class View : MonoBehaviour {
      public State state;
      public Type stateType;
  
      public void SetState (State state) {
          if (state.GetType () == this.stateType) {
              this.state = state;
          }
      }
  }
  </code></pre>
  
  <p>To access the state in <code>ViewA</code> I do this:</p>
  
  <pre><code>(StateA)this.state
  </code></pre>
  
  <p><strong>Solution 2 - The unity way</strong></p>
  
  <p><code>State</code> will be changed to derive from MonoBehaviour.</p>
  
  <pre><code>public abstract class State : MonoBehaviour {}
  </code></pre>
  
  <p>To add a state to an item I would simply add a new <code>StateA</code> component to the gameObject. The item then loads that state with GetComponent to access it.</p>
  
  <pre><code>viewA.gameObject.AddComponent&lt;StateA&gt; (); // set state with fixed type
  viewA.gameObject.AddComponent (type);     // set state dynamically
  
  this.gameObject.GetComponent&lt;StateA&gt; (); // get state in item class
  </code></pre>
  
  <p><strong>Conclusion</strong></p>
  
  <p>Both of these solutions are not ideal in my opinion. Do you guys know of a better way to do this kind of stuff? I hope you understand what I'm trying to achieve. Looking forward to see your ideas.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Seems I oversimplified my question. To make clear why I do it that way think of <code>Item</code> as <code>View</code> in a UI. The <code>StateA</code> does not have any functionality. Instead it's just a number of properties to let the view know what it should do. For a table view for example the state would contain a collection of things to show. </p>
  
  <pre><code>public class StateA : State {
      SomeObject[] tableViewData; // which data to load into the table view
  }
  </code></pre>
  
  <p>Or for another view. </p>
  
  <pre><code>public class StateB : State {
      bool hideButtonXY; //should I hide that button?
  }
  </code></pre>
  
  <p>In another class I keep a history of the last showed views with their respective states. This way I am implementing a back and forth feature from a normal web browser. 
  I hope this makes my intentions clear. </p>