- Home /
Trouble populating 2D array for Match-3 Game
Hi All,
I'm trying to teach myself the mechanics behind a match-3 game and am having some issues handling my arrays. The code below seems to create my 2D array (at least, it gets through the CreateBoard function...) but when I go to try to populate it, things fall apart:
var numSymbols:int = 4; var symbols:Array = new Array(numSymbols); var currentBoard:Array;
//the symbols being used atm.
symbols[0] = "triangle";
symbols[1] = "circle";
symbols[2] = "square";
symbols[3] = "ex";
public function CreateBoard(x:int, y:int):Array {
//create an array with of x elements.
//Each element will be an array of y elements.
var gameBoard = new Array (x);
for (var i in gameBoard)
gameBoard = new Array(y);
print("created");
return gameBoard;
}
// 'a' is never getting a proper variable passed to it, it seems.
public function PopulateBoard(a:Array):Array{
var toPop:Array = a;
for (var i in toPop){
for (var j in toPop[i])
toPop[i] = symbols[Random.Range(0,numSymbols)];
}
print("populated");
return toPop;
}
//I think I broke it. Just fix this, then start testing the output.
function Update(){
if (Input.GetButton("Fire1")){
currentBoard = CreateBoard(5,4);
currentBoard = PopulateBoard(currentBoard);
//for (var i in currentBoard) print(currentBoard);
}
}
The specific error I'm getting is
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible)
Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value)
Boo.Lang.Runtime.RuntimeServices.UnboxInt32 (System.Object value)
gameLogic.PopulateBoard (UnityScript.Lang.Array a) (at Assets/gameLogic.js:24)
gameLogic.Update () (at Assets/gameLogic.js:35)
To me it sounds like the 'a' in PopulateBoard isn't getting passed the proper information but, as far as I can tell, it should be...
I decided to use JS arrays as, from what I understand, unity's built-in ones don't have as much flexibility. Also, I've seen the question here: http://answers.unity3d.com/questions/16431/passing-a-multidimensional-array-in-javascript but I'm trying to understand why my approach wouldn't work :(
Can someone show me the error of my ways?
Thanks in advance and sorry for the massive wall-o-code
Answer by Eric5h5 · Apr 17, 2011 at 10:05 PM
There's no reason to use Array; it's slow and not typesafe. Just use a 2D array (the answer you reference is outdated). Also you should use ints rather than strings, since it's faster and simpler. You can use enums if you want human-readable values instead of magic numbers.
enum Symbols {Triangle, Circle, Square, Ex}
var currentBoard : int[,];
(As to the actual problem, you're assigning gameBoard over again for every iteration through the array, and the "i" in "for (var i in gameBoard)" is never used.)
oh wow, ok. I really was making things harder than they needed to be. Thanks a bunch!
Your answer
![](https://koobas.hobune.stream/wayback/20220613123400im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D array problem in C# 2 Answers
2d array.What is the difference between these two? 1 Answer
What type of Array should I use? 1 Answer
2D array of GameObjects C# 2 Answers