gun manager how to

does anyone know how to make a gun manager script in javascript? if u pick up a gun it adds it to an inventory or something a little like that.i guess u have to add a new input but i dont know where to go from there. i need the gun to be part of my character when he picks it up(i have my own character)

thank you

Basically you just need an array to store the weapons in, from which you can select them. I do it this way (although it's not the prettiest, it allows 1-10 weapons). Basically, if you hit a number key 1-0 it selects a weapon from the array. All you would need to do is have your guns on the character but turned off (invisible/not able to fire) and have functions on the gun to turn them on or off, then if you put it in the weapons array it will be selectable.

var weapons : GameObject[]; 
var selectedWeapon : int;
function Update () {
if (Input.GetKeyDown("1") && weapons.length >= 1) {
    SelectWeapon(0);
    selectedWeapon = 0;
} else if (Input.GetKeyDown("2") && weapons.length >= 2) {
    SelectWeapon(1);
    selectedWeapon = 1;
} else if (Input.GetKeyDown("3") && weapons.length >= 3) {
    SelectWeapon(2);
    selectedWeapon = 2;
} else if (Input.GetKeyDown("4") && weapons.length >= 4) {
    SelectWeapon(3);
    selectedWeapon = 3;
} else if (Input.GetKeyDown("5") && weapons.length >= 5) {
    SelectWeapon(4);
    selectedWeapon = 4;
} else if (Input.GetKeyDown("6") && weapons.length >= 6) {
    SelectWeapon(5);
    selectedWeapon = 5;
} else if (Input.GetKeyDown("7") && weapons.length >= 7) {
    SelectWeapon(6);
    selectedWeapon = 6;
} else if (Input.GetKeyDown("8") && weapons.length >= 8) {
    SelectWeapon(7);
    selectedWeapon = 7;
} else if (Input.GetKeyDown("9") && weapons.length >= 9) {
    SelectWeapon(8);
    selectedWeapon = 8;
} else if (Input.GetKeyDown("0") && weapons.length >= 10) {
    SelectWeapon(9);
    selectedWeapon = 9;
}       
}

function SelectWeapon (index : int) {
for (var i : int=0 ;i<weapons.length; i++)  {
    if (i != index){
        weapons*.gameObject.BroadcastMessage("deselectWeapon");*
 *//the function deselectWeapon is in my gun script, and turns them off.*
 *}else{*
 _weapons*.gameObject.BroadcastMessage("selectWeapon");*_
 _*//the function selectWeapon is in my gun script, and turns them on.*_
 _*}*_
_*}*_
_*```*_
_*<p>}</p>*_

I use C# myself so can't give you any example code but what I would try in this case is to create a list in your characters class where you store all weapons you have picked up.

Then set up a way to swap between the weapons in that list when pressing a button, and for the weapon currently equiped, put it in a temp cache and remove it from the inventory. And set up another button for droping a weapon, for that simply remove the equiped weapon from the cache (since it's already removed from the inventory as that's done when equiping it).

oh and before equiping a new weapon ofcourse you'ld add the currently equiped one back to inventory so we don't have two weapons equiped or accidentally destroy the equiped one completly.

:) And I suppose when pressing the button to swap weapons I'ld probably call another method aswell that would instantiate the prefab/model of the currently equiped weapon at the point where the weapon should be positioned, and then make it a parent of the player character / camera