- Home /
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
Answer by aldonaletto · Dec 11, 2011 at 07:01 PM
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[i].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.
I just want to add that it was ok for curWeapon to be a GameObject since it holds the current instance. Your example won't work since you try to Destroy the curWeapon which is now an int ^^
The error happens because he should have used the int parameter "weapon" of his ChangeWeapon function as index for the array.
But yes, it's also a good idea to hold the current index in case you want a next / previous button ;). Activating / deactivating is also the perfered method since Instantiate and Destroy are quite heavy functions.
For God's sake! I messed things more than the original question! Thanks a lot, @Bunny83. I fixed the answer.
In your first script, Destroy(curWeapon) will try to destroy the int variable. Not the weapon GameObject.
But your second script is O$$anonymous$$. ;)
Thanks, it was a big rubbish (I missed the use of curWeapon in destroy/instantiate - shame on me!) - I've just fixed the answer.
is it possible to see a fully functional fixed version? this is still racking my brain!
Your answer
Follow this Question
Related Questions
Passing a set of variables from one script to another 3 Answers
Multiple Melee Weapons 2 Answers
Bootcamp Weapon Hud & Reloading Problem 2 Answers
switch weapons script not working? 2 Answers
How do you use weapon handling? 0 Answers