Weapon Switching

Well i’m making an FPS and i’m having problens with my switch weapons script…well, at the begginning I had onky 1 gun, an M4, then i was thinking about duplicating this Gameobject (and the script, to make different wep stats), so I duplicated it, changed the model and the stats, i now i have 2 guns, but i disabled the fisrt gun (gameobject, in inspector window) to test the second gun, now, i’m receiving a lot of random erros, and when i play the game, i spawn 2 characters, the one that is already in the scene, and one that falls from space (Player(Clone)), I don’t know what is causing this and idk the solution…here are all the codes u need to read:

M4 Script:

#pragma strict
//Weapon Script

var bullet : GameObject;
var gunBarrel : Transform;
var reloadSound : AudioClip;
var shootSound : AudioClip;
var emptySound : AudioClip;
var hudAmmo : GUIText;
var hudMags : GUIText;

var ammo : int;
static var mags : int;
var fireRate : boolean;
var shootTime : float;

var aimingDownSights : boolean = false;

function Start () {

	ammo = 45;
	mags = 225;
	fireRate = true;
	shootTime = 0;

}

function Update () {

	hudAmmo.text = "Loaded:" +ammo;
	hudMags.text = "Stored:" +mags;

	if(Input.GetButton("Fire1")){
		if(ammo > 0){
			if(shootTime > 0.1) {
				ammo--;
				audio.clip = shootSound;
				audio.Play();
				Instantiate(bullet,gunBarrel.position,gunBarrel.rotation);
				shootTime = 0;
				if(aimingDownSights){
					animation.Play("ADSAnim3");
					} else {
					animation.Play("FireAnim1");
				}
			}
		}
	}
		
	if(ammo == 0 && !audio.isPlaying){
		audio.clip = emptySound;
		audio.Play();	
		
	}
		
	shootTime += Time.deltaTime;
	
	if(Input.GetKeyDown(KeyCode.R)){
		if(ammo == 0 && mags > 0) {
			audio.clip = reloadSound;
			audio.Play();
			animation.Play("ReloadAnim");
			ammo = ammo + 45;
			mags = mags -45;
		}
	}

	if(Input.GetButtonDown("Fire2")){
		aimingDownSights = true;
		animation.Play("ADSAnim1");
	
	}
	
	if(Input.GetButtonUp("Fire2")){
		aimingDownSights = false;
		animation.Play("ADSAnim2");
	}	
}

Desert Eagle Script:

#pragma strict
//Weapon Script

var bullet : GameObject;
var gunBarrel : Transform;
var reloadSound : AudioClip;
var shootSound : AudioClip;
var emptySound : AudioClip;
var hudAmmo : GUIText;
var hudMags : GUIText;

var ammo : int;
static var mags : int;
var fireRate : boolean;
var shootTime : float;

var aimingDownSights : boolean = false;

function Start () {

ammo = 7;
mags = 21;
fireRate = true;
shootTime = 0;

}

function Update () {

hudAmmo.text = "Loaded:" +ammo;
hudMags.text = "Stored:" +mags;

if(Input.GetButtonDown("Fire1")){
	if(ammo > 0){
		if(shootTime > 0.5) {
			ammo--;
			audio.clip = shootSound;
			audio.Play();
			Instantiate(bullet,gunBarrel.position,gunBarrel.rotation);
			shootTime = 0;
			if(aimingDownSights){
				animation.Play("ADSAnim3");
				} else {
				animation.Play("FireAnim1");
			}
		}
	}
}
	
if(ammo == 0 && !audio.isPlaying){
	audio.clip = emptySound;
	audio.Play();	
	
}
	
shootTime += Time.deltaTime;

if(Input.GetKeyDown(KeyCode.R)){
	if(ammo == 0 && mags > 0) {
		audio.clip = reloadSound;
		audio.Play();
		animation.Play("ReloadAnim");
		ammo = ammo +7;
		mags = mags -7;
	}
}

if(Input.GetButtonDown("Fire2")){
	aimingDownSights = true;
	animation.Play("ADSAnim1");

}

if(Input.GetButtonUp("Fire2")){
	aimingDownSights = false;
	animation.Play("ADSAnim2");
}	

}

Ammo Box Script:

#pragma strict
//Ammo Box/Recharger script

var distBox : int;
var rechargeQuant : int;
var skinAmmo : GUISkin;

function Start () {

rechargeQuant = 225;

}

function Update () {

distBox = Vector3.Distance(GameObject.Find("Player").transform.position, transform.position);

	if(Input.GetKeyDown(KeyCode.E) && distBox <= 2){
	
		GunScriptM4.mags = GunScriptM4.mags + 225;
		GunScriptDEagle.mags = GunScriptM4.mags + 21;
		Destroy(gameObject);
	
	}

}

function OnGUI(){

GUI.skin = skinAmmo;

	if(distBox <=2){
	
		GUI.Label(Rect(Screen.width /2 -150,Screen.height /2,500,500), "Press E to recharge your gun");
	
	}
}

PS: All variables are assinged…

Can someone help me??? Please! And i think i’m gonna do the weapon switching script like this:

//just a reference, i know the name are wrong

var m4 : go;
var deagle :go;

function start
  m4.activeSelf = true;
  deagle.activeSelf = false;

function update
  if(press 1)
    PickUpM4();

  if(press 2)
    PickUpDEagle();

function PickUpM4
    animation.Play("pickupm4");
    m4.activeSelf = true;
    deagle.activeSelf = false;

function PickUpDEagle
    animation.Play("pickupdeagle");
    m4.activeSelf = false;
    deagle.activeSelf = true;

//maybe yields and other commands will be added latter, i did't write this script in my project yet

I apologize about my english, thanks…

EDIT: FORGET ABOUT THE WEAPON SWITCHING THING, I GAVE UP ON THAT FOR NOW, BUT THE GAME STILL SPAWNING 2 PLAYERS WHEN I START THE GAME, CAN SOMEBODY FIX THIS OR KNOW HOW TO?

You shouldn’t duplicate the weapon script, instead you should create one generic (with the variable that exists for every weapon) and then create the other ones by inheriting from it. Besides that, the “weapon switching” script is full of errors: all the functions in there are missing both the round parenthesis after their names and the braces surrounding their body, also the functions “start” and “update” must be capitalized (letter case is important).

var m4 : GameObject;
 var deagle : GameObject;
 
 function Start () {
   m4.activeSelf = true;
   deagle.activeSelf = false;
}
 
 function Update () {
   if (Input.GetKeyDown (KeyCode.Alpha1))
     PickUpM4();
 
   if (Input.GetKeyDown (KeyCode.Alpha2))
     PickUpDEagle();
}
 
 function PickUpM4 () {
     animation.Play("pickupm4");
     m4.activeSelf = true;
     deagle.activeSelf = false;
}
 
 function PickUpDEagle () {
     animation.Play("pickupdeagle");
     m4.activeSelf = false;
     deagle.activeSelf = true;
}

PS: if it’s not too late I’d suggest to switch to C# instead of Javascript, much better for readability and probably more supported by Monodevelop with better intellisense.

Already have the same problem. I try solve it by create separate weapon prefab with all scipts like weapon damage, weapon type, weapon modifiers etc. Then attach to player instance all scripts with prefab weapons and set only one of them to active. Switch weapon script simply get current active component disable it and enable component with desired weapon prefab. I’ve only 3 components of weapon: one meele and two range types. One of them is still active rest are disabled.