Is there a way to make a 2 dimensional array in Unity? basically an array of arrays, that you could access by say:
grid[desired x position][desired y position] = 2;
Or barring that could I do an actual array of arrays?
Thanks
Is there a way to make a 2 dimensional array in Unity? basically an array of arrays, that you could access by say:
grid[desired x position][desired y position] = 2;
Or barring that could I do an actual array of arrays?
Thanks
this is untested, but as it uses javascript, it should work to initialize the array with arrays, e.g.:
a=new Array(3);
for (var i = 0; i < a.length; i++) {
a[i] = new Array(2);
}
this way you should be able to set your data with
a[1][2]=“something”;
and read through a[×][y]
and you can always go for the oldschool way:
arraywidth=2;
a=new Array(arraywidth*3); // initialize 2 columns 3 rows
value=a[arraywidth*y+x]; // read value
And what about three dimensional array.
I tried:
static var tt : float[,,];
but this way doesn’t seems to work.
Just in case you need it in C#…
object[,] = new object[x,y]
x y in the initializer set the range.
So if you wanted to create a multidimensional array of int’s 10 x 10.
int[,] = new int[10,10]
You can do for each on the array which is nice for an array of objects with properties.
foreach(Player player in PlayersArray[,])
{
if(player.Position == tile.Position)
{
blah blah
}
}
I really can’t get over this. After reading all posts containing “array” ( a lot), looking at mono documentation and various sources on the web, still my code doesn’t work.
I just need to declare, initialize and retrieve floats from a static 3 dimensional (not staggerd) builtin .net array, using JScript.
I need builtin cause every object in my game, each rockets that gets shooted, has to look in this table to initialize various so it can’t be slow.
I’m totally stuck and would appreciate help a lot.
Unfortunately you can’t; you have to use C#. I’d recommend using the bug reporter to request it; the more they see this, the more likely they are to implement it, I hope. (Seems to me that it should be fairly trivial to implement too, considering that these arrays can already be used in Javascript, just not declared.)
In the meantime, if you don’t want to use C#, you can use a 1D array and treat it as a 3D array with some math, which is how they work behind the scenes anyway.
–Eric
Thanks so much Eric5h5. You just avoided me reading another thousand posts. Will try to use some math on a single dimension js array. Are you sure there’s a way to read the array from js correctly once declared and initialized in c#? Many thanks anyway, I think I finally understand the problem.
Not that I’m aware of; all scripts that used the multi-dimensional array would need to be C#. You can use multi-dimensional arrays in JS when using functions like GetHeights, but I never found a way of defining multi-d arrays in C# scripts and using them in JS scripts.
–Eric
I solved it with a monodimensional array and some trivial math, Now it works fine. The bebug logger iin Unity is good. Is able to put out some 20,000 logs entry in less than a quarter second. It’s good to have it, even if would like so much a real debugger… Thanks for your help again.
Tell me more about the “old school” … I’m losing a lot of hours to implement it in a 3d array as Unity debug tools are limited.
Anyway, I’m @ the formula x+(yyWidth)+(zxWidth*yWidth)…
I’m testing it in these days… if you know the correct formula and don’t bother writing it down for me, you’ll save me hours of tedious work. (Well, playing with math is not too much boring, but after this I’m ready to move to level design so I wanna get there fast).
Many thanks anyway, even if you don’t answer.
This code worked for me. In theory could work with n dimensions …
var level : Array = new Array( new Array(1,1,1,1),new Array(1,1,1,1),new Array(1,1,1,1),new Array(1,1,1,1));
var x : int = 0;
var y : int = 0;
for ( var row : Array in level )
{
x++;
for ( var column : int in row )
{
y++;
if( y == 1 )
{
// code
}
}
}
sorry, loop code fixed:
var x : int = 0;
var y : int = 0;
for ( var row : Array in level )
{
x++;
y = 0;
for ( var column : int in row )
{
y++;
if( column == 1 )
{
//code
}
}
}
Well, It’s working since a month now…
static var tt : float[];
static var xZero : int = 0;
static var rockets : int = 1;
static var mortar : int = 2;
static var artillery : int = 3;
// ... and on till 16
static var xSize = 17;
//then y part
static var yZero : int = 0;
static var shotForce : int = 1; //the x force of the round shot
static var roundLifeTime : int = 2; //how much the round can stay live
/// ... and on till 34
static var ySize = 35;
//z part (3rd dimension)
//no nees to define constants here, ranging from 0 to 99 (the maximum tech level)
static var zSize = 100;
static var zWidth = xSize*ySize;
//and the real array init code
tt = new float[zWidth*zSize]; //builtin array redefined
//...
//...
//...
//then I access vars this way
tt[speed+(xSize*engineSpeed)+(0*zWidth)] = 2.0; // *
// where "speed" is x index, "engineSpeed" is y index and the value for which I multiply zWidth is z index.
//all of this happens inside a fiel whre most everything is "static", so I can access and modify the array from all of my scripts.
This solved the problem for me, but it’s really annoying not to be able to specify a multidimensional array straight from js.
If you would like unity to add support for javascript built in multi dimensional arrays, you can vote for that feature by going here:
http://feedback.unity3d.com/pages/15792-unity/suggestions/163605-scripting-improve-javascript-features-properties-et
I was under the impression that multi dimensional arrays are a commonly used programming feature. It Seems odd that Unitys version of JavaScript does not currently support it. Lets see if we can help encourage the unity staff to add this feature for us.