- Home /
Question by
Captain_Mayonnaise · Jul 30, 2014 at 04:06 PM ·
javascriptarray
2 Dimensional Array doesn't seem to work
Title says it all, here's the code:
var grid = new Array();
var terrainSize = GetComponent(Terrain).terrainData.size;
for (var i = 0; i < terrainSize.x; i++) {
grid.Push(new Array());
for (var j = 0; j < terrainSize.z; j++) {
grid[i].Push(false);
}
}
And when I try to run it I get the following error:
Assets/Scripts/TerrainManagement.js(9,33): BCE0019: 'push' is not a member of 'Object'.
Why are arrays not being created in the spaces in the grid 1D array?
Sorry, I've probably done something stupid but I'm pretty new to JS so.
Comment
Best Answer
Answer by Eric5h5 · Jul 30, 2014 at 04:16 PM
Never use the Array class; it's slow and untyped. If you want a 2D array, use an actual 2D array, especially since that's what TerrainData functions require:
var grid = new float[sizeX, sizeY];
Thank you! This is exactly what I wanted anyway but I found loads of things saying that JS has no way of doing it like that. Tried so many things :D Thanks