- Home /
Multidimensional array in Javascript
I'm trying to get a multidimensional array working. I have:
// Global variable grid of Cards (Cards is my own class)
var aGrid : Cards[,];
function Start(){
// Init aGrid
for( var i = 0; i < 4; ++i ){
for( var j = 0; i < 4; ++i ){
aGrid[ i, j ] = new Card();
}
}
}
function accessGrid(){
// Try to access initialized aGrid
// This bombs, saying aGrid is a null value
var card : Card = aGrid[ intX, intY ];
}
When I run the app, the accessing of aGrid fails saying aGrid is null, even though the intX and intY indexing variables are valid (i.e., are values between 0<4).
Didn't I initialize aGrid correctly in the Start() function? Do I need some type of aGrid = new Card[4,4]; statement?
if it is a fixed array give the size of it throught editor!
Answer by StevenCalwas · Mar 01, 2012 at 09:31 PM
Actually, while writing the original question, I came up with the correct answer. I added the statement:
aGrid = new Card[ 4, 4 ];
to the Start() function and everything works.
Thanks for taking the time to post your answer to this, I am working though the same problem in the Unity book and was looking for a "#pragma strict" way to do this for the old 3.x code.
If this is the answer, please accept it so the question shows up as answered :)
I attempted to do that, but says I do not have permission. I am guessing there is some sort of waiting period before I can mark questions answered.
@_Gloam Only an Ad$$anonymous$$/$$anonymous$$oderator or the person who asked the question can accept an answer. $$anonymous$$y comment was towards @StevenCalwas
@Benproductions1 Gotcha, upvoting was giving me the trouble I suppose. Looks like I am short on karma AT$$anonymous$$.