How to get objects with a specific component and store them in an array.

So, i made this code to switch weapons, and all i’m trying to do is get how much weapons the player has, so i know how to set the array, and then, put all weapons into the array, however, how do i get the objects with the “WeaponFire” component?

BTW, this is not an FPS, nor a TPS, it is a car combat game.

Code:
#pragma strict

var currentWeapon = 0;
var Weapons : WeaponFire[];

function getSelectedWeapon() : WeaponFire
	{
	return Weapons[currentWeapon];
	}


function Start()
	{
	
	if (currentWeapon >= Weapons.length) currentWeapon = Weapons.length-1;
	
	for (var i=0; i<Weapons.length; i++)
		DisableWeapon(Weapons*);*
  • SelectWeapon(Weapons[currentWeapon]);*
  • }*

function Update () {

  • if (Input.GetKeyDown(KeyCode.PageUp))*

  •  SwitchWeapon(-1);*
    
  • if (Input.GetKeyDown(KeyCode.PageDown))*

  •  SwitchWeapon(1);*
    

}

function SwitchWeapon (iDir : int)

  • {*

  • if (Weapons.length < 2) return;*

  • DisableWeapon(Weapons[currentWeapon]);*

  • currentWeapon += Mathf.Sign(iDir);*

  • if (currentWeapon < 0) currentWeapon = Weapons.length-1;*

  • else if (currentWeapon >= Weapons.length) currentWeapon = 0;*

  • SelectWeapon(Weapons[currentWeapon]);*

  • }*

function DisableWeapon(Weapon : WeaponFire)

  • {*
  • Weapon.weaponSelected = false;*
  • }*

function SelectWeapon(Weapon : WeaponFire)

  • {*
  • Weapon.weaponSelected = true;*
  • }*

WeaponFire wfs = GameObject.FindObjectsOfType(typeof (WeaponFire)) as WeaponFire;

And if you want to put the associated gameobjects in a seperate array:

GameObject[] weapons = new GameObject[wfs.Length];

for(int i = 0; i < weapons.Length; i ++) {
     weapons _= wfs*.gameObject;*_

}

Thanks for the answear, much appreciated, i ended up creating a very different setup for dynamic weapons, i simply made an empty and all weapons are attached to this empty, so i just count the number of child objects of this empty and i get the exact number of weapons. So very easy to set te array then.