- Home /
adding "X" amount of objects to an array
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[i]);
print("This is your XArray: " + tileArrayX[i]);
}
}
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
This is really confusing. What you have now is two arrays with 10 elements each; is that what you actually want, or should it be a bidimensional 10x10 array, thus containing 100 elements and covering completely a rectangular area (from the name tile, that's what I suspect you actually need).
yes that is what i wanted an multidimensional array, but i couldn't figure out how to put the x's in one and the y's in another. so i decided to go with two seperate arrays ins$$anonymous$$d, but i do need it to be 10x10. so i am restricting it some how?
Answer by SilverTabby · Sep 08, 2011 at 02:53 AM
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];
yeah somebody was telling me this earlier, but i couldn't figure out how to implement it into what i was doing. and all around i dont understand it ha ha. like the part where you say myArray.Length(0); i dont understand what that code is doing at all.
This code is just the basics you need to create and use 2-d arrays.
If you understand 1-d arrays, this code is all you need to add on the second dimension.
the .GetLength(N) is how the .NET library returns the length of the Nth dimension of a multi-dimensional array (because .length doesn't return enough information)
this really did help alot lol my thinking was completely wrong on this, but ive figured it out now thanks!
Answer by simonmc · Sep 08, 2011 at 02:51 AM
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.
this will add all of the game objects to one array though. the problem i have with this is its a lot harder to define the x and y values of each, which for what i am doing i really need.
Your answer
Follow this Question
Related Questions
Object reference not set to an instance of an object with object set, 1 Answer
Instantiate object in for loop with array 2 Answers
Can't assign an instantiated class to an array? 1 Answer
each instantiation getting it's own draw call? 1 Answer
A null value was found where an object instance was required. 1 Answer