Convert data type from inherited classes in C#
<p>I am trying to understand inheritance for my unity project but seem to have found a limitation to my setup. I got my self confused whilst writing it as i am still learning to understand C# properly.</p>

<p>I have a set of classes that inherit, and they split based on two different behaviors that way i have the correct reference.</p>

<p>I then need to cast them so i can have access to a method in one of these classes. So my structure looks like this:</p>

<pre><code>public class Behaviour : Position {
    public Handler reference;

    public Behaviour(int tx, int ty, Handler refer) : base (tx,ty){
        reference = refer;
    }
    // overload
    public Behaviour(int tx, int ty) : base (tx,ty){}
}

public class Behaviour2 : Position {
      public SettingsHandler reference;

    public Behaviour2(int tx, int ty, SettingsHandler refer) : base (tx,ty) {
        reference = refer;
    }
}


public class SettingsHandler : Handler {
    public Settings level {get;set;}
}
public class Handler : MonoBehaviour{
    virtual public void Enter(List&lt;Node&gt; n,Vector3 p){}
    virtual public void Exit(List&lt;Node&gt; n, Node curNode){}
}
</code></pre>

<p>Now this was working fine until i had to access Handler.Enter or Handle.Exit. Then i got lost on how to set the type properly.</p>

<p>So I was doing something like this:</p>

<pre><code>//need to set temp :

 ??? temp;
if(path[i] is Behaviour2){
   temp = (Behaviour2)path[i];
} else {
   temp = (Behaviour)path[i];
}
temp.reference.Enter();
</code></pre>

<p>What should temp type be set to here?</p>

<p>I am thinking i might have misunderstood inheritance as i seem to get type issues. Does C# have a solution for this - i can't be the only one who has got stuck. But my brain is getting confused trying to follow it all.</p>