[SOLVED] Cycling all the way through weapons properly

Thanks to the amazing ardizzle for solving this! To me, working through this post is EXACTLY what this community is all about!

Edit: You know, I realize that maybe I’m asking the wrong question, and making this far too complicated. Let me ask this:

Is there a way to call a function multiple times in the same script? Right now, if I try I get a “StackOverflow” error.

The gist of my post below is I have three weapons. (According to the script, four, but one of them is simply an empty hand) By pressing the “Weapon Switching” button on the gamepad the weapon switches to the next weapon, ironically enough. The issue is that when I get to the final weapon it stays on that rather than cycling back to the first one (Empty hand) and starting the process over. So my latest thought is if I could just call the original function again (SelectWeapon, or even Update) in the script below, that might work. I can’t believe how incredibly frustrated I am by this, it seems like it would be such a simple thing to do, but this issue has dogged me for a month now. [END OF EDIT]

Hello again,

I’ve been having some success using a weapon switching script, but need to cycle completely through the player’s weapons. Right now player is able to cycle from 1 - 4, but then stays at weapon 4. I need them to be able to restart at 1 once they have 4 equipped, and have pressed the “weapon switching” controller button. I tried calling “Update” again, only to get a stack overflow. (ok, that makes sense, but was willing to try it) Any help is greatly, and humbly, appreciated. God bless.

var Weapon01 : GameObject;
var Weapon02 : GameObject;
var Weapon03 : GameObject;
var Weapon04 : GameObject;
static var weaponready = false;
static var swordready = false;
static var cameraready = 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);
	Weapon04.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);
		Weapon04.SetActiveRecursively(false);
		}
		if (Input.GetButtonDown("Weapon Switch")) {
		if (cameraready == true)
		CameraSelect();
	}
	}
	
	
	
	                function CameraSelect () {
	                if (Weapon03.active == true)
	                {
	                if (WeaponInv.Camera >=1)
	                if (cameraready == true)
	               
	    
		            Weapon01.SetActiveRecursively(false);
		            Weapon02.SetActiveRecursively(false);
		            Weapon03.SetActiveRecursively(false);
		            Weapon04.SetActiveRecursively(true);
		            }
		            if (Input.GetButtonDown("Weapon Switch")) {
		            Update();
		            }
	                }

Ok man. I wrote you up a script that should work. You might have to make some small changes to make it work for your project. I just made 4 game objects and made it turn them on and off in an order. Here it is :

#pragma strict

var Weapon01 : GameObject;
var Weapon02 : GameObject;
var Weapon03 : GameObject;
var Weapon04 : GameObject;
// Check to see if you have these weapons yet.
var obtainedWeapon03 : boolean = false;
var obtainedWeapon04 : boolean = false;
static var weaponready = true;
static var swordready = false;
static var cameraready = false;
static var equipSlot : int = 1;

function Start()
{
	WeaponsFalse(); // Makes sure you don't have more than one weapon set.
	Weapon01.SetActive(true);// Makes sure you start the game with your first weapon
}
function Update () {
    if(Input.GetMouseButtonUp(0))// You will have to change to what you need 
    {
        if(weaponready == true)
        {
        	WeaponChange();
        }
    }    
}
    function WeaponChange()
    {
	    if(Weapon01.activeSelf == true)
	    {
	    	// Changes to the second weapon
	    	WeaponsFalse();
	   	 	Weapon02.SetActive(true);
	   		Debug.Log ("Second Weapon");
	    }
	
	    else if(Weapon02.activeSelf == true)
	    {
	    	// If you have the 3rd weapon it switches else goes back to 1st weapon
	    	if(obtainedWeapon03 == true)
	    	{
	    		WeaponsFalse();
	    		Weapon03.SetActive(true);
	    		Debug.Log ("Third Weapon");
	    	}
	    	else
	    	{
	    		Debug.Log("Sorry you only have 2 weapons");
	    		WeaponsFalse();
	    		Weapon01.SetActive(true);
	    	}
	    }
	
	    else if(Weapon03.activeSelf == true)
	    {
	    	// If you have the 4rd weapon it switches else goes back to 1st weapon
	    	if(obtainedWeapon04 == true)
	    	{
	    		WeaponsFalse();
	    		Weapon04.SetActive(true);
	    		Debug.Log ("Fourth Weapon");
	    	}
	    	else
	    	{
	    		Debug.Log("Sorry you only have 3 weapons");
	    		WeaponsFalse();
	    		Weapon01.SetActive(true);
	    	}
	    }
	
	    else if(Weapon04.activeSelf == true)
	    {
	    	// Goes back to the 1st weapon
	    	WeaponsFalse();
	    	Weapon01.SetActive(true);
	    	Debug.Log ("First Weapon");
	    }
    }

    function WeaponsFalse()
    {
    	// Deactivates all weapons to avoid having more than one weapon active at once
	    Weapon01.SetActive(false);
	    Weapon02.SetActive(false);
	    Weapon03.SetActive(false);
	    Weapon04.SetActive(false);
    }

I also added some variables so that if you don’t have the 3rd weapon or the 4th weapon it will set you back to the first weapon. What do you think?