Problem accessing a class array

Hi everyone!

I’ve searched the forums but couldn’t get this to work…
I’m trying to set some variables in an array of a class type, but none of the suggestions I’ve found worked :frowning:

Basically what I’m trying to do is:

Javascript

public var columns = 10;
public var rows = 5;

class SomeClass {
   public var xIndex : int;
   public var yIndex : int:
}

function Start () {
   myArray = new SomeClass[(columns*rows)];
   for (var x=0; x < columns; x++) {
      for (var y=0; y < rows; y++) {
         myArray[x][y].xIndex = x;
         myArray[x][y].yIndex = y;
      }
   }
}

I’ve tryed casting, using built-in and standard arrays but nothing seems to work…
The only solution I can imagine would be either rewrite the entire script in C# (although I’ve never wrote a line of C#) or put everything inside a 1D array and offset the addresses (and that would screw up my math in the remaing code). Is really that hard to make bidimensional arrays in Unity? Any ideas? Thanks!

Cheers!

replace

[x][y]

with

[x*rows + y]

you don’t have an array of arrays there, so you can’t access it like one as well.

Sorry, I miswrote the code in my original post…

It should go as follows:

public var columns = 10; 
public var rows = 5; 

class SomeClass { 
   public var xIndex : int; 
   public var yIndex : int: 
} 

function Start () {
   //build an array of type SomeClass with 10 elements
   myArray = new SomeClass[columns]; 
   for (var x=0; x < columns; x++) {
      //every element is also an array of type SomeClass with 5 elements
      myArray[x] = new SomeClass[rows];
      for (var y=0; y < rows; y++) { 
         myArray[x][y].xIndex = x; 
         myArray[x][y].yIndex = y; 
      } 
   } 
}

I tryed so many things to make get this right, I’ve got confused :sweat_smile:
Anyway, I’m trying to build and access a bidmensional array of type “SomeClass” but failling with every effort. Anymore ideas? Thanks!

Will still not work.
Because myArray[×] will return object, not Array
You would need to cast the return first to use it as array.

automagic is not available on JS on the iphone.
you now need to do the work in your scripts you expect it to do.

Not sure though if this works at all for JS on iphone, as I primarily use C#