Game Nearly Complete need some advice

Hi there and thanks for looking the question I have is this. I have a game where you can unlock 11 weapons I was wandering if there is an easier way to do it compared to the way I have also with a mouse scroll weapon selection. The way I have it I am sure is not optimised and i’m sure it wont work the more weapons I add all i want is some advice on the best way to do this whether that be separate weapon scripts or what. Here is the code not finished but want advice before i carry on

var electroDrill : boolean = true;

var rayGun1 : boolean = false;

var rayGun2 : boolean = false;

var rayGun3 : boolean = false;

var laser : boolean = false;

var laser2: boolean = false;

var laser3 : boolean = false;

var bazooka : boolean = false;

var freezeGun : boolean = false;

var frogGun : boolean = false;

var emp : boolean = false;



var electroDrillModel;

var rayGun1Model;

var rayGun2Model;

var laser1Model;

var laser2Model;

var laser3Model;

var bazookaModel;

var freezeGunModel;

var frogGunModel;

var empModel;



// Weapon variables



// ElectroDrill

var eLECTRODRILLINSP : String;

var shootForce : float;

var bullet : GameObject;

var bulletHolder : Transform;

var ammo : int;

var canShootElectroDrill : boolean;

// RayGun

var rayGunForce : float;

var rayGunBullet : GameObject;

var rayGunBulletHolder : Transform;

var rayGunAmmo : int;

var canShootRayGun : boolean = true;







function Start()

{

	electroDrillModel = gameObject.Find("ElectroDrill").renderer.enabled = true;

	rayGun1Model = gameObject.Find("Raygun_lvl1").renderer.enabled = false;

}

	

				

							

										



function Update()

{

	if(electroDrill == true)

		{

			ElectroDrill();

		}

	if(rayGun1 == true)

		{

			RayGun1();

		}

	

}



function ElectroDrill()

{

	if(electroDrill == true && PauseGame.paused == false)

	{

		if(Input.GetButton("Fire1") && ammo > 0 && canShootElectroDrill == true)

				{

					canShootElectroDrill = false; // can we shoot

					animation.CrossFade("Gun");

					//AudioSource.PlayClipAtPoint(bulletSoundGun, transform.position);//AUDIO

					var shoot = Instantiate(bullet, bulletHolder.position, bulletHolder.localRotation); // instantiate the bullet at the bulletHolderGunCam's position and rotation

					shoot.rigidbody.AddForce(transform.forward * shootForce);//add force to the bullet prefab // BULLET \\

					ammo -= 1; // take 1 away from our ammo

					yield WaitForSeconds(0.2); // wait 1 seconds so that the animations can get reset 

					canShootElectroDrill = true; // we can now shoot again;

				}

	}

}



							

function RayGun1()

{

	if(rayGun1 == true && PauseGame.paused == false)

		

		{

			rayGun1Model.renderer.enabled = true;

			if(Input.GetButton("Fire1") && rayGunAmmo > 0 && canShootRayGun == true)

				{

					canShootRayGun = false;

					animation.CrossFade("Gun");

					var shootRay = Instantiate(rayGunBullet, rayGunBulletHolder.position, rayGunBulletHolder.localRotation);

					shootRay.rigidbody.AddForce(transform.forward * rayGunForce);

					rayGunAmmo -= 1;

					yield WaitForSeconds(0.2);

					canShootRayGun = true;

				}

		}

}	





	



	

		

			

				

// Reminder Create the scripts for the rest of the weapons here
function LateUpdate()

{

	if(rayGunAmmo <= 0)

		{

			rayGunAmmo = 0;

		}

		

	if(ammo <= 0)

		{

			ammo = 0;

		}

		

	if(electroDrill == true)

		{

			rayGun1 = false;

			rayGun2 = false;

			rayGun3 = false;

			laser = false;

			laser2 = false;

			laser3 = false;

			bazooka = false;

			freezeGun = false;

			frogGun = false;

			emp = false;

		}

	if(rayGun1 == true)

		{

			electroDrill = false;

			rayGun2 = false;

			rayGun3 = false;

			laser = false;

			laser2 = false;

			laser3 = false;

			bazooka = false;

			freezeGun = false;

			frogGun = false;

			emp = false;

		}

		

		if(rayGun2 == true)

		{

			electroDrill = false;

			rayGun1 = false;

			rayGun3 = false;

			laser = false;

			laser2 = false;

			laser3 = false;

			bazooka = false;

			freezeGun = false;

			frogGun = false;

			emp = false;

		}

	if(rayGun3 == true)

		{

			electroDrill = false;

			rayGun1 = false;

			rayGun2 = false;

			laser = false;

			laser2 = false;

			laser3 = false;

			bazooka = false;

			freezeGun = false;

			frogGun = false;

			emp = false;

		}

		

		if(laser == true)

		{

			electroDrill = false;

			rayGun1 = false;

			rayGun2 = false;

			rayGun3 = false;

			laser2 = false;

			laser3 = false;

			bazooka = false;

			freezeGun = false;

			frogGun = false;

			emp = false;

		}

	if(laser2 == true)

		{

			electroDrill = false;

			rayGun1 = false;

			rayGun2 = false;

			rayGun3 = false;

			laser = false;

			laser3 = false;

			bazooka = false;

			freezeGun = false;

			frogGun = false;

			emp = false;

		}

		

		if(laser3 == true)

		{

			electroDrill = false;

			rayGun1 = false;

			rayGun2 = false;

			rayGun3 = false;

			laser = false;

			laser2 = false;

			bazooka = false;

			freezeGun = false;

			frogGun = false;

			emp = false;

		}

	if(bazooka == true)

		{

			electroDrill = false;

			rayGun1 = false;

			rayGun2 = false;

			rayGun3 = false;

			laser = false;

			laser2 = false;

			laser3 = false;

			freezeGun = false;

			frogGun = false;

			emp = false;

		}

		

		if(freezeGun == true)

		{

			electroDrill = false;

			rayGun1 = false;

			rayGun2 = false;

			rayGun3 = false;

			laser = false;

			laser2 = false;

			laser3 = false;

			bazooka = false;

			frogGun = false;

			emp = false;

		}

	if(frogGun == true)

		{

			electroDrill = false;

			rayGun1 = false;

			rayGun2 = false;

			rayGun3 = false;

			laser = false;

			laser2 = false;

			laser3 = false;

			bazooka = false;

			freezeGun = false;

			emp = false;

		}

		

		if(emp == true)

		{

			electroDrill = false;

			rayGun1 = false;

			rayGun2 = false;

			rayGun3 = false;

			laser = false;

			laser2 = false;

			laser3 = false;

			bazooka = false;

			freezeGun = false;

			frogGun = false;
		}
}

I recommend learning about classes and arrays.

Your project is screaming for you to define a class to represent a gun.

Here is an example of what that might look like:

class Gun
{
    var name          :  String;
    var isSelected    :  boolean;
    var canUserShoot  :  boolean;
    var shootForce    :  float;
    var ammo          :  int;
    var bullet        :  GameObject;
    var bulletHolder  :  Transform;
}

If you define a class named Gun like that, you can then put the following at the top of your script:

var gunList : Gun [];

This will declare an array of guns that you can maniplulate in your script. You can edit the guns in the array in the Inspector as well.

Learning about arrays is a must. They are one of the most important, useful, and fundamental concepts of programming.

Once you've defined your gun class and put some data in it, you can process the array in your script like this:


for ( var thisGun : Gun in gunList ) {
    print(thisGun.name + " has this power: " + thisGun.shootForce);
}

You have a crazy amount of variables that are similar. Like RayGun1, RayGun2, RayGun3. You could make a class or just a boolean list (sorry not familiar with javascript), so if you ever wanted to create RayGun4 it would be easy. And also instead of setting each one to false, you could just loop through each RayGun in your RayGun list.

In C# it would be like so

List<bool> RayGuns = new List<bool>();
RayGuns.Add("RayGun1")
RayGuns.Add("RayGun2")

and when you wanted to set them all to false you could just write

foreach(bool b in RayGuns)
{
b = false;
}

but you could take this one step further if you created a Gun Class, and then create a class that holds all the Guns, so you would have all your guns (RayGuns, Lasers) in one list, allowing easy setting to true or false.