- Home /
List of Arrays?
Hi, all!
I constructed a grid for my enemies to use for positioning a while ago. The grid is a series of Vector3's expressed in [ row, column ] form.
Well, I want to spawn randomly selected sets of enemies at specified places in the grid, so I made an enum called EnemyType which has 4 types: Probe, Tank, Bomb, and Null.
For easy placement on the grid, I made temporary variables in the start function that reference these types ( P, T, B, and o for null ).
I need to make an array of these 2D arrays so I can select individual [row, column] enemy sets to be iterated through and spawned.
However, Unity seems to either disagree with the idea of a [,,], or I just don't know how to write it.
Is this wrong?
#pragma strict
@HideInInspector
var easySets : EnemyType[,,];
//Creating contaners for our SetArrays
function Start () {
var P : EnemyType = EnemyType.Probe;
var B : EnemyType = EnemyType.Bomb;
var T : EnemyType = EnemyType.Tank;
var o : EnemyType = EnemyType.Null;
// Setting up quick-access, one-key variables to create sets
//EasySets = new EnemyType[2,7,7];
easySets = [//START EASY SETS
[P, o, o, o, o, o, P,
o, P, o, P, o, P, o,
o, o, P, o, P, o, o,
o, o, o, o, o, o, o,
o, o, P, o, P, o, o,
o, P, o, P, o, P, o,
P, o, o, o, o, o, P],
[P, o, o, o, o, o, P,
o, P, o, P, o, P, o,
o, o, P, o, P, o, o,
o, o, o, o, o, o, o,
o, o, P, o, P, o, o,
o, P, o, P, o, P, o,
P, o, o, o, o, o, P]
]; //END EASY SETS
}
If so, how should I write it? I just want to be able to extract a single 2D Array, iterate through it, and spawn enemies accordingly. The iteration and spawning part I already know how to do, so really I just need help on expressing it.
Thanks a ton! - YA
Answer by whydoidoit · May 21, 2013 at 02:42 AM
So your initialization there is creating a jagged array of EnemyType:
var array : EnemyType[][];
This isn't what you were expecting and I don't think that there is a syntax to initialise a multi-dimensional array in any case.
If you wanted to make an array that was 3d jagged var array: EnemyType[][][]
then you would need to initialize it like this:
easySets = [
[
[P, o, o, o, o, o, P],
[o, P, o, P, o, P, p],
...
],
...
];
This you could access using easySets[x][y][z] rather than easySets[x,y,z];
Oh. So you are saying that there isn't a manual way to populate a multidimensional array save through iteration?
Jagged array is fine by me as well. However, whenever I type the statement
var easySets : EnemyType[][][];
I get an error thrown at me about the semicolon at the end. I don't think the compiler is expecting or reading [][][]...
This: http://answers.unity3d.com/questions/54695/how-to-declare-and-initialize-multidimensional-arr.html
tells me that I can't set the type of a jagged array. Thanks for the help! That was the final piece of the puzzle.
Just in case anyone was interested, I wrote a script that would create a square 2D array from a single array of elements (you have to specify how big the square array is in the i and u for loops but it works).
function $$anonymous$$akeASet( myArray : EnemyType[] ){
var my$$anonymous$$ultiArray : EnemyType[,] = new EnemyType[7,7];
var q = 0;
for( var i = 0; i < 7; i++ ){
for( var u = 0; u < 7; u++){
my$$anonymous$$ultiArray[i,u] = myArray[ q ];
q++;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613102630im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Talking Code 2 Answers
Arrays with zero length 1 Answer
help with score display concept 2 Answers
Can't get rid of errors 0 Answers
For Loop Array, skip 1 value 2 Answers