How to have flexible data type for inherited classes

Hello

I have a set of inherited classes and i want to cast my data depending on the data involved. So this is my setup currently:

public class Behaviour : Objects {
    public Handler reference;

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

public class Behaviour2 : Objects {
      public SpecialHandler reference;

    public Behaviour2(int tx, int ty, SpecialHandler ref) : base (tx,ty) {
        reference = ref;
    }
}


public class SpecialHandler : Handler {
    public Settings level {get;set;}
}
public class Handler : MonoBehaviour{
    virtual public void Enter(List<Node> n,Vector3 p){}
    virtual public void Exit(List<Node> n, Node curNode){}
}

And then i am trying to do something like this:

//need to set t here so i don't lose scope

???   temp;
if(myObjects[i] is Behaviour){
   temp = (Behaviour)path[i];
} else if (data[i] is Behaviour2){
   temp = (Behaviour2)path[i];
}
t.reference.Enter(result,position);

So the problem here is how do i set my type for temp before i assign it and then i can call the method ? They both will have access to .Enter method but i need to cast the type first so i have the correct reference.

Hope you can help explain as i am confused.

You would usually have the temp variable be a base class. or you can use Generics with or, you can not setup a temp and just pass in the path directly to the Enter() method.

It wouldn’t let me do it directly, i had to cast first for some reason =/

just use a generic class or method, you can just pass in the needed type as a type parameter.

Where exactly in the code do you mean ? Not sure i understood.