Okay So Im Wondering How Do I Add The Next Weapons Down The Line. I want At Least 5,
I Cant Seem To Think How To Make The Third And So On Show. I thought else if But It Didnt Work.
#pragma strict
var Weapon01 : GameObject;
var Weapon02 : GameObject;
var Weapon03 : GameObject;
function Update ()
{
if(Input.GetAxis("Mouse ScrollWheel") > 0)
{
SwitchWeapons();
}
}
function SwitchWeapons()
{
if(Weapon01.active == true)
{
Weapon01.SetActiveRecursively(false);
Weapon02.SetActiveRecursively(true);
Weapon03.SetActiveRecursively(false);
}
else
{
Weapon01.SetActiveRecursively(true);
Weapon02.SetActiveRecursively(false);
Weapon03.SetActiveRecursively(false);
}
}
When you scroll the wheel, in the update function your CurrentWeapon variable needs to change. Otherwise it is always 0, so only that index of the array will be set active.
Here is a sample of a more optimised(untested) code, which does away with the for loop in the Update function(which is never a good idea to have in the first place)
var WeaponsArray : GameObject[];
var CurrentWeapon : int;
var PreviousWeapon : int;
function Start()
{
CurrentWeapon =0;
//Deactivate all weapons initially
for(var i=0; i<WeaponsArray.length; i++) WeaponsArray[i].SetActive(false);
//Activate the first weapon we want the player to have
WeaponsArray[CurrentWeapon].SetActive(true);
}
function Update ()
{
if(Input.GetAxis("Mouse ScrollWheel") > 0)
{
CurrentWeapon = ((CurrentWeapon + 1) >= WeaponsArray.length) ? WeaponsArray.length-1 : CurrentWeapon + 1;
WeaponsArray[CurrentWeapon].SetActive(true);
if(PreviousWeapon != CurrentWeapon)
WeaponsArray[PreviousWeapon ].SetActive(false);
PreviousWeapon = CurrentWeapon;
}
else
{
CurrentWeapon = ((CurrentWeapon - 1) < 0) ? 0 : CurrentWeapon - 1;
WeaponsArray[CurrentWeapon].SetActive(true);
if(PreviousWeapon != CurrentWeapon)
WeaponsArray[PreviousWeapon ].SetActive(false);
PreviousWeapon = CurrentWeapon;
}
}