Switch Weapons Problem

var Primary : GameObject;
var Secondary : GameObject;
var third : GameObject;
var fourth : GameObject;

function Start ()
{
Primary.SetActiveRecursively(true);
Secondary.SetActiveRecursively(false);
third.SetActiveRecursively(false);
fourth.SetActiveRecursively(false);




}

function Update ()
{

if(Input.GetKeyDown("1") && Secondary.active == true){
Primary.SetActiveRecursively(true);
Secondary.SetActiveRecursively(false);
third.SetActiveRecursively(false);
fourth.SetActiveRecursively(false);

}

if(Input.GetKeyDown("2") && Primary.active == true){
Primary.SetActiveRecursively(false);
Secondary.SetActiveRecursively(true);
third.SetActiveRecursively(false);
fourth.SetActiveRecursively(false);

}


if(Input.GetKeyDown("3") && third.active == true){
Primary.SetActiveRecursively(false);
Secondary.SetActiveRecursively(false);
third.SetActiveRecursively(false);
fourth.SetActiveRecursively(true);

}


if(Input.GetKeyDown("4") && fourth.active == true){
Primary.SetActiveRecursively(false);
Secondary.SetActiveRecursively(false);
third.SetActiveRecursively(true);
fourth.SetActiveRecursively(false);

}


}

Acutally Am Switch Weapons here , what wrong with this script ?? can you explain
thank you in advance

I would do it easier like this:

function Start()
{
SelectWeapon(1);
}

function Update(){
    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);
}
}

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);
}
}

Attach this script to the parent of the Weapons. Every Weapon is one child.

In my game I use a two weapons thing and you can change them by picking other ones up. If you are interested in such a script I can post some lines of it to give you an idea. Just write it in a comment if so.

I know I didn’t debug Your script but if I were you I would do it more compact.

I hope this helps you

Here I will explain how I make it so that you have two weapons on 1 and 2 and can replace them by picking up others.

So what you have to do beforehand is setting up several tags with the names of the weapons like for example “AK”, “M16”,…

The weaponshave to be a child of either an empty gameobject or the camera like it is in for example the FPS tutorial.

Now you have to set the weapons to the fitting tags. (Btw. In my game I have an empty gameobject called “Weapons” which is a child of the playercamera. The Weapons are children of “Weapons”. Every weapon consist of an empty gameobject with the shoting script. The weaponmodel and the necessary particles are children of this gameobject. Just to clear things up).

Then you make prefabs of Weaponmodel to pick up. The prefabs also has to have the fitting tags.

So here is my PlayerWeapons script which is attached to the “Weapons”-GameObject. I changed it slightly because I have some stuff in it which is not important for you.

static var PrimaryWeapon="AK";
static var SecondaryWeapon="DesertEagle";
static var SwitchWeapon=1;
function Start () {
	
	// Select the first weapon
    SelectWeapon(0);
	PlayerCharContr= Player.GetComponent("CharacterController");
}

function Update () {

 //Here I deleted all of the stuff from Firing the Weapons to Crouching, Grenade-Throwing,...

	if(Input.GetKeyDown("1"))
	{
		SwitchWeapon=1;
            SelectWeaponTag(PrimaryWeapon);
	}
	if(Input.GetKeyDown("2"))
	{
			SwitchWeapon=2;
                    SelectWeaponTag(SecondaryWeapon);
	}
}

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);
			transform.GetChild(i).gameObject.BroadcastMessage("ChangeWeapon");
		// Deactivate all other weapons
		}
		else
			transform.GetChild(i).gameObject.SetActiveRecursively(false);
	}
}

function SelectWeaponTag (tag: String) {
	for (var i=0;i<transform.childCount;i++)	{
		// Activate the selected weapon
		if (transform.GetChild(i).gameObject.tag==tag)
		{
			print(tag);
			transform.GetChild(i).gameObject.SetActiveRecursively(true);
			transform.GetChild(i).gameObject.BroadcastMessage("ChangeWeapon",    SendMessageOptions.DontRequireReceiver);
		// Deactivate all other weapons
		}
		else
			transform.GetChild(i).gameObject.SetActiveRecursively(false);
	}
}

I hope I didn’t deleted anything important or left something unimportant in.

Now here is the script to Pickup Weapons:

var WeaponsArray= new Array ();

var WeaponPickuppref: GameObject;
function Start () {

WeaponsArray.length= transform.childCount;
for (var i=0;i<transform.childCount;i++) {
WeaponsArray*= transform.GetChild(i).tag;*

  •   }*
    

}
function UseFunc () {

  • var hit : RaycastHit;*
  • var cam : Transform = Camera.main.transform;*
  • var layerMask : int = 1 << 8;*
  • layerMask = ~layerMask;*
    _ if (Physics.Raycast (cam.position+cam.transform.forward.normalized*1, cam.forward, hit, 5, layerMask))_
  • {*
  •  	if(hit.rigidbody)*
    
  •  	var SelectedWeapon = hit.rigidbody.tag;*
    
  •  	//print(SelectedWeapon);*
    
  •  	//Destroy(hit.rigidbody.gameObject);*
    
  •  for (var i=0;i<transform.childCount;i++)	{*
    

_ if(SelectedWeapon==WeaponsArray*){*_

* ThrowWeapon(cam);*
* if(PlayerWeapons.SwitchWeapon==1)*
* {*
* PlayerWeapons.PrimaryWeapon=SelectedWeapon;*
* }*
* else if(PlayerWeapons.SwitchWeapon==2)*
* {*
* PlayerWeapons.SecondaryWeapon=SelectedWeapon;*
* }*
* transform.SendMessage(“Pickupfunc”, SendMessageOptions.DontRequireReceiver);*
* Destroy(hit.rigidbody.gameObject);*
* }*
* }*
* } *
}

function ThrowWeapon(cam:Transform){
* var rotation=Quaternion(cam.forward.x,cam.forward.y,cam.forward.z,0);*

* if(PlayerWeapons.SwitchWeapon==1)*
* {*
* for(var p=0;p<WeaponPickuppref.length;p++){*
* if(WeaponPickuppref[p].tag==PlayerWeapons.PrimaryWeapon){*
_ var ThrowedWeapon1:Rigidbody=Network.Instantiate(WeaponPickuppref[p].rigidbody, cam.position+cam.transform.forward.normalized2, rotation,0);
ThrowedWeapon1.AddForce(cam.forward
10);
* }*
* }*
* }*
* if(PlayerWeapons.SwitchWeapon==2)*
* {*
* for(var p2=0;p2<WeaponPickuppref.length;p2++){*
* if(WeaponPickuppref[p2].tag==PlayerWeapons.SecondaryWeapon){*
var ThrowedWeapon2:Rigidbody=Network.Instantiate(WeaponPickuppref[p2].rigidbody, cam.position+cam.transform.forward.normalized2, rotation,0);
ThrowedWeapon2.AddForce(cam.forward
10); _

* }*
* }*
* } *
}
Here I also hope it works because I had to delete some stuff in this one too.
I really hope this helps if something is missing or is not working then let me know and I see what I can do.
Good Luck!

var Primary : GameObject;
var Secondary : GameObject;
var third : GameObject;
var fourth : GameObject;

function Start ()
{
Primary.SetActiveRecursively(true);
Secondary.SetActiveRecursively(false);
third.SetActiveRecursively(false);
fourth.SetActiveRecursively(false);




}

function Update ()
{

if(Input.GetKeyDown("1") && Secondary.active == true){
Primary.SetActiveRecursively(true);
Secondary.SetActiveRecursively(false);
third.SetActiveRecursively(false);
fourth.SetActiveRecursively(false);

}

if(Input.GetKeyDown("2") && Primary.active == true){
Primary.SetActiveRecursively(false);
Secondary.SetActiveRecursively(true);
third.SetActiveRecursively(false);
fourth.SetActiveRecursively(false);

}


if(Input.GetKeyDown("3") && fourth.active == true){
Primary.SetActiveRecursively(false);
Secondary.SetActiveRecursively(false);
third.SetActiveRecursively(false);
fourth.SetActiveRecursively(true);

}


if(Input.GetKeyDown("4") && third.active == true){
Primary.SetActiveRecursively(false);
Secondary.SetActiveRecursively(false);
third.SetActiveRecursively(true);
fourth.SetActiveRecursively(false);

}

}

I Hope This Work’s