How would I write this?

Hi, say i have a script called “GlobalVars.js” with global variables in it like -

// --------------- ENEMY TYPES -----------------
public var enemyType : int[] = new int[10];		// The Type of Enemy .. Type1, Type2 .. ect up to a maximum of 10 Types
// ---------------------------------------------

// --------------- ENEMY VARIABLES -----------------
public var DistanceToPlayer : float[] = new float[10];							// Distance to the player
public var MusicPlaying : boolean[] = new boolean[10];							// Is the attack music playing for this Enemy
public var PlayerAngle : float[] = new float[10];								// The angle between the Enemy and the Player
public var HitByRPG : boolean[] = new boolean[10];								// Is the Enemy taking a hit by a RPG
public var HitByGrenade : boolean[] = new boolean[10];							// Is the Enemy taking a hit by a Grenade
public var HitGrenadeRPGCount : int[] = new int[10];							// How many times hit by a Grenade or RPG
public var numberOfTheEnemyTypes : int;											// Enemy1, Enemy2, Enemy3, ect ...
public var Health : float[] = new float[10]; 									// Set an array of the Enemy's Health
public var Alive = Enumerable.Range(0,10).Select(function(c) true).ToArray();	// Is the Enemy alive
public var Shooting : boolean[] = new boolean[10];								// Is the Enemy shooting set to false
public var BeingShot : boolean[] = new boolean[10];								// Is the Enemy being shot
public var TypesBonus : int[] = new int[10];									// Bonus points for killing a Enemy
public var killigEnemyHealthBonus : int[] = new int[10];						// Bonus health for killing a Enemy

And I have 4 enemy 2 named -

“Enemy Type 1” with tags of “Enemy 1” and “Enemy 2”

and 2 named -

“Enemy Type 2” with tags of “Enemy 1” and “Enemy 2”

each with the same scrip attached, how would I set a variable from their script, say I want to set the Health variable on the “GlobalVars.js” scrip of “Enemy Type 1” with tag of “Enemy 2”

This is how I thought I would do it -

#pragma strict

import System.Linq; // This is needed for the "function ConvertToInt" and "ConvertToInt(transform.tag)" to work

private var globalVars : GameObject;	// The GameObject with the GlobalVars.js containing all the Variables for the game
private var enemyTagNumber : int;
private var enemyTypeNumber : int;
private var enemyHealth : float = 100;

function Start () {

	enemyTagNumber = ConvertToInt(transform.tag);	// Get the Enemy Tag number from it's tag
	enemyTypeNumber = ConvertToInt(transform.name);	// Get the Enemy Type number from it's name
	globalVars = GameObject.Find("Global Vars");

// **** THIS IS HOW I THOUGHT IT WHOULD BE DONE ****
	globalVars.GetComponent(GlobalVars).enemyType[enemyTypeNumber].Health[enemyTagNumber] = enemyHealth;
}

// ---------------------------------------------------------------
// Function to extract the numer from the EnemyX.tag or .name
// ---------------------------------------------------------------
function ConvertToInt(stringContainingNumber : String) : int{
   return System.Convert.ToInt32(stringContainingNumber.Substring(stringContainingNumber.ToList().FindIndex(function(c) char.IsDigit(c))));
}
// ---------------------------------------------------------------

But it says ‘Health’ is not a member of ‘int’. any help would be great, thanks.

What I have seen posted for this sort of thing is to make a var and do a GetComponent look up as little as possible. As I am guessing your doing more than one look up to get the stuff you wanted.

var globalVarsScript : GlobalVars; // var name : script name as the Type
gobalVarsScript = golbalVars.GetComponent(GlobalVars);


gobalVarScript.enemyType[enemyNumber] = enemyTypeNumber;
gobalVarScript.Health[enemyNumber] = enemyHealth;