Can I make an array of scripts?

Can I make an array of scripts?

I have a global variable script (GlobalVars.js) that hold all my variables for my game, two of them being the currentWeapon and the other currentWeaponNumber that holds the current weapon being used and the number of the weapons position as the child of an empty game object called weapons, each weapon has a script called AnimationCtrlWeapon.js on it

public var currentWeapon : Transform;
public var currentWeaponNumber : int;

I’ve made an array of scripts (the way I thought it should be done) in the GlobalVars.js like below.

public var weaponScriptArray : Array = new Array(7);

public var Bazooka : GameObject;
public var G36c : GameObject;
public var HandGun : GameObject;
public var M16Scope : GameObject;
public var PPSH41 : GameObject;
public var ShotGun : GameObject;
public var Grenade : GameObject;

function Start(){
	
// Find all the weapons
	Bazooka = GameObject.Find("Bazooka");
	G36c = GameObject.Find("G36c");
	HandGun = GameObject.Find("Hand Gun");
	M16Scope = GameObject.Find("M16 Scope");
	PPSH41 = GameObject.Find("PPSH41 Gun");
	ShotGun = GameObject.Find("ShotGun Without Light");
	Grenade = GameObject.Find("Stick Grenade");

// Populate weaponScriptArray with the weapons AnimationCtrlWeapon script
	weaponScriptArray[0] = Bazooka.GetComponent(AnimationCtrlWeapon);
	weaponScriptArray[1] = G36c.GetComponent(AnimationCtrlWeapon);
	weaponScriptArray[2] = HandGun.GetComponent(AnimationCtrlWeapon);
	weaponScriptArray[3] = M16Scope.GetComponent(AnimationCtrlWeapon);
	weaponScriptArray[4] = PPSH41.GetComponent(AnimationCtrlWeapon);
	weaponScriptArray[5] = ShotGun.GetComponent(AnimationCtrlWeapon);
	weaponScriptArray[6] = Grenade.GetComponent(AnimationCtrlWeapon);
}

Then in a script called button.js I tried to access the current weapons AnimationCtrlWeapon.js

In buttons.js I have

private var globalVars : GameObject;
private var globalVarsScript : GlobalVars;

function Start(){
	globalVars = GameObject.Find("Global Vars");
	globalVarsScript = globalVars.GetComponent(GlobalVars);
}

So I thought to alter say the bulletCount variable in the current weapons AnimationCtrlWeapon.js I would use

globalVarsScript.weaponScriptArray[globalVarsScript.currentWeaponNumber].bulletCount = 6;

But I get the error message - ‘bulletCount’ is not a member of ‘Object’.

I can access the variable with

globalVarsScript.currentWeapon.GetComponent(AnimationCtrlWeapon).bulletCount = 6;

But was trying to miss out GetComponent()

Anyone know what wrong please? Is it because the weaponScriptArray has to be set as type : Script? Thank you.

weaponScriptArray Should be an array of AnimationCtrlWeapon.

Try something like :

var weaponScriptArray : List.<AnimationCtrlWeapon>;

This works quite well for me. You even get to see the list in the inspector.

var uiLabels : List.<UILabel> = new List.<UILabel>();
  
function Start(){
	var labels : UILabel[] = GameObject.FindObjectsOfType(UILabel) as UILabel[];
	for (var label : UILabel in labels) {
		uiLabels.Add(label);
	}
}

3 years late but for people who need this;
This is how i add scripts to an array or list and with one script i can disable or enable them… hit like if you like :slight_smile:

public MonoBehaviour[] disabledOnes;

public void Update(){
if (disabledOnes.Length > 0)
    {
        foreach (MonoBehaviour disableThis in disabledOnes)
        {
            disableThis.enabled = false;
        }
    }

}