Array: Is there a more efficient way?

Hi

I’ll try to be brief on this.
What I’m trying to achieve is to move many (eventually 30-odd) enemies into the scene into a set formation.
The destinations for the formation will be eventually random, but for now I’m just fixing them until i get this code right.
Each destination is specified by a reference to an empty gameObject’ transform in the scene. Each of these objects i’ve named 1-50. I grab these GOs with:

var t1grid : Transform[];

I then turn ‘of’ those transforms in the grid i don’t want my enemies to go to with t1grid[1] = false; and so forth for each of the 50.

Now, to move the enemies, I do the following. My question is, is there a more efficient way? As you can see below, I only have 3 moving into position, and this all works correctly. But if I want to move 30, then i’ll have 30 cases… and I wondered if there was a more efficient way of somehow accessing the t1grid array, and cycliing through them all, assigning their name to the iTween as well.

t1g1 = true;   t1g2 = true;   t1g3 = true;

var enemy : Transform;
	
for (enemy in PoolManager.Pools["Enemy1Pool"]) {
		
		switch (true) {
			case t1g1:
				destination = t1grid[1];
				t1g1 = false;
			break;
		
			case t1g2:
				destination = t1grid[2];
				t1g2 = false;
			break;
		
			case t1g3:
				destination = t1grid[3];
				t1g3 = false;
			break;
			default:
			break;
		}
		iTween.MoveTo(enemy.gameObject,{"x": destination.localPosition.x, "y": destination.localPosition.y, "time": UnityEngine.Random.Range(0.5, 2), "easetype": iTween.EaseType.easeInOutCubic});
	
}

Thanks for any advice you can offer on this. If there’s no other way, then fine I’ll write out the 30… just wanted to check before I go and do so!
Cheers

just wanted to add a side note to this. I wondered if theres any way of grabbing the array reference for each transform as using that? ive looked up the manual on arrays but i always get errors like cant turn transform into int etc.

Instead of a bunch of separate variables, use a boolean array.

As an aside, there’s no point doing this:

var enemy : Transform;
	
for (enemy in PoolManager.Pools["Enemy1Pool"]) {

“enemy” doesn’t exist outside the loop, so “var enemy : Transform” is just sitting there unused. You can remove that line. (Sure would be nice if they brought back the unused variable warnings when using JS that used to exist.)

–Eric

Thanks Eric, I’ll remove that line.

I’m away from the project at the moment, but if I do something like this, would it work?

var gridArray : boolean[];
     gridArray.Add.t1grid[];

… or do i get those gameObject.transform references into the array some other way? (The GOs sitting in t1grid[ ] ).
I always get so confused with arrays lol. I need the reference to the transforms so that i can move the enemy to the transform position.

EDIT: At the project now and this obviously doesn’t work.

Arrays like boolean[ ] can’t be resized.

gridArray = new boolean[20]; // or however many

–Eric

This is what I have so far. The enemies move, but only to the first position in the grid. Now and then (odd occasion), they will move to fill positions 1 2 in the grid, but the third enemy is going into position 2 as well. Obviously, the setting of the grid position to False is occuring too late or something for enemies to pick up on it (even though that wasn’t the case in my first example script above.

var gridArray = new boolean[30]; 

gridArray[1] = true;
gridArray[2] = true;
gridArray[3] = true;
//Assign the spawned enemies to a grid location.
	for (enemy in PoolManager.Pools["Enemy1Pool"]) {
//gridArray	check
		for(i = 1; i < caf1EnemyTotal+1; i++) {	
			if (gridArray[i] == true) {
				gridArray[i] = false;
				destination = t1grid[i];
				//Debug.Log(t1grid[i]+"..arrived at "+gridArray[i]);
				
				} else{
				Debug.Log("False = going to next position in grid");
				}
				iTween.MoveTo(enemy.gameObject,{"x": destination.localPosition.x, "y": destination.localPosition.y, "time": UnityEngine.Random.Range(0.5, 2), "easetype": iTween.EaseType.easeInOutCubic});
		}
			
	}
	
}

Not sure what to do next. Should i be (somehow) removing the grid marker from the boolean array once it’s hit?