Weapon switching

i have this code from the fps toutorial

it works perfect

now i want to switch more than 2 weapons how can ideal with it

i tried many things but nothing worked

here is the script

function Start () 
{
   // Select the first weapon
   SelectWeapon(0);
}

function Update () 
{
   // Did the user press fire?
   if (Input.GetButton ("Fire1"))
      BroadcastMessage("Fire");
   if (Input.GetKeyDown("1")) 
   {
      SelectWeapon(0);
   }    
   else if (Input.GetKeyDown("2")) 
   {
      SelectWeapon(1);
   }    
}

function SelectWeapon (index : int) 
{
    for (var i=0;i<transform.childCount;i++)
    {

// Activate the selected weapon

if (i == index)

transform.GetChild(i).gameObject.SetActiveRecursively(true);

// Deactivate all other weapons

else
            transform.GetChild(i).gameObject.SetActiveRecursively(false);
    }
}

Well, from the code you've posted, it looks like you'd need to:

  1. Add a new weapon as a child object of your player

    (Since the code assumes that each child of the player object is a weapon option, and this is how it accesses them)

  2. Modify the 'if..else' part of you script so that instead of:

    if (Input.GetKeyDown("1")) 
    {
        SelectWeapon(0);
    }
    else if (Input.GetKeyDown("2")) 
    {
        SelectWeapon(1);
    }
    
    

You add some more key options, like this:

    if (Input.GetKeyDown("1")) 
    {
        SelectWeapon(0);
    }
    else if (Input.GetKeyDown("2")) 
    {
        SelectWeapon(1);
    }
    else if (Input.GetKeyDown("3")) 
    {
        SelectWeapon(2);
    }
    else if (Input.GetKeyDown("4")) 
    {
        SelectWeapon(3);
    }

If this doesn't work, post a more detailed description of the problems you're having with it, and I'll try to help further.

Duck seems to be right, what you could also do, is just have 2 as go to the next weapon, 1 go back a weapon. Like this:

var wepNum : float = 0;
var numOfWeps : float = 5;
//whatever the rest is
function Update () {
SelectWeapon(wepNum);
}
if(Input.GetKeyDown("1")) 
{
if(wepNum > 0) 
wepNum--;
}
if(Input.GetKeyDown("2"))
{
if(wepNum < numOfWeps) {
wepNum++;
}
//Rest of code required

if (Input.GetKeyDown(“1”))
{
SelectWeapon(0);
}
else if (Input.GetKeyDown(“2”))
{
SelectWeapon(1);
}
else if (Input.GetKeyDown(“3”))
{
SelectWeapon(2);
}
else if (Input.GetKeyDown(“4”))
{
SelectWeapon(3);
}
else if (Input.GetKeyDown(“6”))
{
SelectWeapon(5);
}

Here Is A Script To Switch To 5 Weapons Don’t Assign Ticks :smiley: I Recommend You 1-Knife.2-Pistol.3-Assault Rifle.4-Machine Gun.5-Sniper :smiley: :smiley: GOOD LUCK X_X

var tick1 : AudioClip;
var tick2 : AudioClip;
var tick3 : AudioClip;
var tick4 : AudioClip;
var tick5 : AudioClip;


function Start () {
   // Select the first weapon
   SelectWeapon(0);
}

function Update () {
   // Did the user press fire?
   if (Input.GetButton ("Fire1"))
      BroadcastMessage("Fire");
   
   if (Input.GetKeyDown("1")) {
      audio.PlayOneShot(tick1);
      SelectWeapon(0);
   }   
   else if (Input.GetKeyDown("2")) {
      audio.PlayOneShot(tick2);
      SelectWeapon(1);
   }
   else if (Input.GetKeyDown("3")) {
      audio.PlayOneShot(tick3);
      SelectWeapon(2);
   }
   else if (Input.GetKeyDown("4")) {
      audio.PlayOneShot(tick4);
      SelectWeapon(3);
   }
   else if (Input.GetKeyDown("5")) {
      audio.PlayOneShot(tick5);
      SelectWeapon(4);
   }   
      
}

function SelectWeapon (index : int) {
   for (var i=0;i