While I'm On The Topic Of Casting-- Polymorphism

Hey-- here is what I’m doing-- it worked for me, but I wasn’t sure if it was /right/.

class Player extend MonoBehaviour
{
  private var isHuman;
  public function SetIsHuman (is : boolean)
  {
    isHuman = is;
  }

  public function IsHuman () : boolean
  {
    return isHuman;
  }
}

class HumanPlayer extends Player
{
  public function SomethingHumanSpecific : void {}
}

class AIPlayer extends Player
{
  public function SomethingAISpecific () : void {}
}

class GameController extends MonoBehaviour
{
  var aPlayer : Player;

  function  Update ()
  {
    if (aPlayer.IsHuman())
    {
       var humanPlayer : HumanPlayer = aPlayer.GetComponent(HumanPlayer) as HumanPlayer;
       humanPlayer.SomethingHumanSpecific();
    }
   else
    {
       var aiPlayer : AIPlayer = aPlayer.GetComponent(AIPlayer) as AIPlayer;
       aiPlayer.SomethingAISpecific();
    }
  }
}

Is there a better way of doing this (maybe an instanceof)?

That will work but do you really want to dig yourself that deep in a class hierarchy? Unity (and many other game engines) push your towards component based design patterns rather than the OO/subclassing pattern. Why not just make a component for each behavior you want? This makes it easier to make changes later because you don’t have to worry about affecting the entire class hierarchy.

Well, I think I do.

I can take another look at the code and see if I am really reusing things. I think I am, but I could be naive.