Setting the Type of a variable at runtime

I have a class called BossMovement that I’m using as a universal movement script for multiple bosses. Each boss has it’s own real movement script/class which is named after itself. I have a variable in the BossMovement class that needs to hold a reference to the real movement class for the specific boss that it’s attached to. The problem is that each of the different real movement classes is just that, a different class, thus they each have a different type. I tried to set the reference variable as a MonoBehaviour, but ran into the typecasting problem of not knowing or having a way to hold onto the type of class that’s being set. Here’s an example,

// Universal Movement script
public class BossMovement : MonoBehaviour {
      MonoBehaviour referenceScript;

      public void Setup(MonoBehaviour script)
      {
            referenceScript = script;
      }
}
// Boss 1's Real Movement script
public class BossOneMovement : MonoBehaviour {
      void Start()
      {
            ((BossMovement)gameObject.GetComponent("BossMovement")).Setup(this);
      }
}
// Boss 2's Real Movement script
public class BossTwoMovement : MonoBehaviour {
      void Start()
      {
            ((BossMovement)gameObject.GetComponent("BossMovement")).Setup(this);
      }
}

Boss1 has his own GameObject with BossOneMovement and BossMovement as components and Boss2 has his own GameObject with BossTwoMovement and BossMovement as components.

Is there a working/better way for the BossMovement class to reference the movement script of the Boss it’s currently attached to (without having a switch statement with a string of that boss’s name, each time I need to make a reference)?

Use class inheritance; make your BossXMovement scripts extend BossMovement.

If B extends A, and you do this on an object with a B attached:

var anAObject = GetComponent(A);

You’ll get a reference but it will try to call B’s versions of functions before A’s.

So, are you saying not to have BossMovement on the objects at all? That returns to the original problem of external scripts not having a Universal script to call. Right now I have multiple external scripts getting the gameObject and then calling BossMovement to either slow or stop the boss. If I have different movement scripts for each boss, this same problem will occur inside each of the external scripts instead of just BossMovement.

No, he said you should use inheritance.

public abstract class BossMovement : MonoBehaviour {
    public abstract void Move(Vector3 position); // no method body because it's abstract
}

public class BossAMovement : BossMovement {
    public override void Move(Vector3 position) {
        // add your code for Boss A movement
    }
}

public class BossBMovement : BossMovement {
    public override void Move(Vector3 position) {
        // add your code for Boss B movement
    }
}


BossMovement boss = GetComponent<BossMovement>(); // you don't have to know if it's BossAMovement or BossBMovement type, BossMovement abstract class makes sure that Move method is always implemented

boss.Move(new Vector3(10, 20, 30)); // Move the boss to the position x=10, y= 20 z=30

Something like this, then?

class BossMovement {
  static function Setup( boss : BossMover ) {
    // do stuff
  }
}
class BossMover extends MonoBehaviour {
  function Start() { Init(); }
  function Init() { BossMovement.Setup( this ); }
}
class Boss1Mover extends BossMover {
  function Init() {
    super.Init(); // calls BossMover.Init()
    // do other stuff
  }
}
class Boss2Mover extends BossMover {
  function Init() {
    super.Init();
    // do different other stuff
  }
}