The code i have right now only goes until 9 on each X and Y array. what i want to know is how would i continue it ( ill explain more after the code)
var instantiateObject : Transform;
var gridX = 10;
var gridY = 10;
function Start () {
//create two new arrays for the x and y axis
tileArrayY = new ArrayList();
tileArrayX = new ArrayList();
for ( var x = 0; x < gridX; x++){
for (var y = 0; y < gridY; y++){
var myName = Instantiate(instantiateObject, Vector3(x *.5,0,y * .5), Quaternion.identity);
myName.name = "x: " + x + "y: " + y;
// assign each y and each x to the individual arrays
if( y % gridY == 0 ){
tileArrayY.Add(myName.gameObject);
}
if( x % gridX == 0 ){
tileArrayX.Add(myName.gameObject);
}
}
}
// check those arrays
for( var i = 0; i < 20; i++){
print("This is your YArray: " + tileArrayY*); *
_ print("This is your XArray: " + tileArrayX*);_
_ }_
_}_
so what i am getting from this code is:
Xarray:
x:0 y:0
x:1 y:0
x:2 y:0
x:3 y:0
_…_
x:8 y:0
x:9 y:0
Yarray:
x:0 y:0
x:0 y:1
x:0 y:2
x:0 y:3
_…_
x:0 y:8
x:0 y:9
but i want them to do this instead:
Xarray:
x:0 y:0
_…_
x:9 y:0
x:0 y:1
_…*_
x:9 y:1
i hope this makes sense? ask me to clarify if you dont understand it and ill be happy to help you help me
thanks
simonmc
2
Changing the loop to:
for ( var y = 0; x < gridY; x++){
for (var x = 0; y < gridX; y++){
var myName = Instantiate(instantiateObject, Vector3(x *.5,0,y * .5), Quaternion.identity);
myName.name = "x: " + x + "y: " + y;
tileArrayX.Add(myName.gameObject);
}
}
Will give you one array, tileArrayX of the form you said. Note that the y loop has been moved to the outside.
I would stay away from the Class-based Arrays in Unity (and in general) - the classes like “Array” and “ArrayList”. I have had nothing but trouble with them.
Here is the basic syntax for working with built-in multi-dimensional arrays in JavaScript, which I have never had any problems with:
var myArray : int[,] = new int[5,5];
myArray[0,1] = 42;
for(var X : int = 0; X < myArray.GetLength(0); X++)
for(var Y: int = 0; Y < myArray.GetLength(1); Y++)
{
myArray[X,Y] = X+Y;
Debug.Log(myArray[X,Y]);
}
var threeDimensionalArrayOfObjects : GameObject[,,] = new GameObject[9,6,42];