multiple weapons at run time

hi, im currently working on this code

var weapons: GameObject[];
private var curWeapon: GameObject;

function ChangeWeapon(weapon:int){
  if (curWeapon){
    Destroy(curWeapon);
  }
  curWeapon = Instantiate(weapons[curWeapon],transform.position,transform.rotation);
}

function Update(){

  if (Input.GetKeyDown("1")) ChangeWeapon(0); 
  if (Input.GetKeyDown("2")) ChangeWeapon(1);
  if (Input.GetKeyDown("3")) ChangeWeapon(2);
}

// FIN

Basically i am trying to change between weapons when a key is pressed, however i keep getting this error

" InvalidCastException: Cannot cast from source type to destination type.
Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value)
Boo.Lang.Runtime.RuntimeServices.UnboxInt32 (System.Object value)
weapons.ChangeWeapon (Int32 weapon) (at Assets/scripts/enemy/score/weapons.js:10)
weapons.Update () (at Assets/scripts/enemy/score/weapons.js:16)
"

any ideas? thankyou

EDITED: You’re using curWeapon as an index, but it’s a GameObject. Like @Bunny83 said, you should use weapon instead, like below:

var weapons: GameObject[];
private var curWeapon: GameObject;

function ChangeWeapon(weapon:int){
  if (curWeapon){
    Destroy(curWeapon);
  }
  curWeapon = Instantiate(weapons[weapon],transform.position,transform.rotation);
}

There’s an interesting alternative to Destroy/Instantiate the weapons: activate the selected weapon and deactivate the others, like this:

var weapons: GameObject[];
private var curWeapon: int = 0; // now curWeapon is an int!

function ChangeWeapon(weapon:int){
  curWeapon = weapon;
  for (var i = 0; i < weapons.length; i++){
    weapons*.SetActiveRecursively(i == weapon);*
 *}*
*}*
*
*

In this case, you must drag the weapon prefabs to the scene, adjust their positions and drag them to the weapons array. You must also call ChangeWeapon(initial-weapon-number) at Start to select the first weapon (or no weapon at all, if you pass -1 to ChangeWeapon). The main advantage of this method is the freedom to adjust each weapon position independently of the others. Another advantage: weapon script variables like ammo count will not be lost each time you change weapons.