- Home /
Trying to cycle through weapons
So after days of pounding my head into my keyboard, and with a lot of help from Miziziziz and Griffo I finally was blessed to get the weapon switch script I was using to work nearly flawlessly. Now the player won't have a weapon until they actually pick it up. Sounds simple, but I was amazed how involved the process was. Thanks to everyone who helped! There's a small issue at the tail end now, that I'm hoping is easy to fix and that I'm just missing: The weapons properly cycle once through each of the gameobjects. (empty hand, club, sword, and then back to empty hand.) But then from the second cycle weapon 2 is always skipped (club) so that it now goes empty hand, sword, empty hand. Am hoping someone might point out what I'm doing wrong. Thanks, and God bless.
var Weapon01 : GameObject; // empty hand
var Weapon02 : GameObject; // club
var Weapon03 : GameObject; //sword
static var weaponready = false;
static var swordready = false;
function Update () {
if (Input.GetButtonDown("Weapon Switch")) {
if (weaponready == true)
SelectWeapon();
}
}
function SelectWeapon () {
if (Weapon01.active == true)
{
if (WeaponInv.Club >=1)
Weapon01.SetActiveRecursively(false);
Weapon02.SetActiveRecursively(true);
Weapon03.SetActiveRecursively(false);
}
if (Input.GetButtonDown("Weapon Switch")) {
if (WeaponInv.Sword >=1)
if (swordready == true)
SwordSelect();
}
}
function SwordSelect () {
if (Weapon02.active == true)
{
if (WeaponInv.Sword >=1)
if (swordready == true)
Weapon01.SetActiveRecursively(false);
Weapon02.SetActiveRecursively(false);
Weapon03.SetActiveRecursively(true);
}
else
{
if (Weapon03.active == true)
{
Weapon01.SetActiveRecursively(true);
Weapon02.SetActiveRecursively(false);
Weapon03.SetActiveRecursively(false);
}
}
}
I'm presu$$anonymous$$g you're starting off with the empty hand, and can pick up objects in any order, so that weapon 2 could either be the club or the sword? If that's the case, does the problem occur irrespective of which weapon is in slot 2?
And are you printing the active weapon to the debug.log?
haven't used the debug.log yet. You are correct as to being able to pick up the weapons in any order, but their placement is static. Slot 1 is always listed as empty, 2 as club, 3 as sword. No matter what order they're picked up in, as soon as the sword is equipped the first time the club can never be equipped again. It goes from the desired order of empty - club - sword to empty - sword. Something in the script is making the cycle skip the club entirely.
Answer by Pecek · Oct 18, 2013 at 11:09 AM
You could just create an array of game objects(the weapons), and cycle through the elements of it(when the player scrolls up or something), that way you can add more weapons later without pain, and the code would look a lot more cleaner.
thanks Peck. I walked away from arrays last week when I found it was a major pain to save them (and their items) into PlayerPrefs, so I was hoping to avoid them, if possible =(
Your answer