- Home /
Question by
ufus09 · Apr 03, 2019 at 05:24 PM ·
2d arrayfillmatchgenerating
Filling the board in a match 3 game,Match 3, generating code
Hello. I am obviously new to unity and i have problems with generating a board for a match 3 game. The code used for generating the board goes like this:
public class Board : MonoBehaviour
{
public GameObject[] dots; //array of the dots
public int width; //width of the matrix
public int height; //height of the matrix
public GameObject tilePrefab; //background image gameobject
private BackgroundTile[,] allTiles; //2d array of all backgrounds
public GameObject[,] allDots; //2d array of all dots
void Start()
{
allTiles = new BackgroundTile[width, height];
allDots = new GameObject[width, height];
SetUp();
}
private void SetUp() //fills the arrays
{
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
Vector2 tempPosition = new Vector2(i, j);
GameObject backgroundTile = Instantiate(tilePrefab, tempPosition, Quaternion.identity) as GameObject;
backgroundTile.transform.parent = this.transform;
backgroundTile.name = "(" + i + "," + j + ")";
int dotTouse = Random.Range(0, dots.Length);
GameObject dot = Instantiate(dots[dotTouse], tempPosition, Quaternion.identity);
dot.transform.parent = this.transform;
dot.name = "(" + i + "," + j + ")";
allDots[i, j] = dot;
}
}
}
}
But the dots which are supposed to be on place [1,height] are placed in [0,height] and i don't know what to do about it
Comment
After a short look i cant find any mistakes in your code.
Can you post a picture of the created board here?
Do you mean the dots are in the wrong position in the array or have the wrong position in the 3D scene?
I just tested your code and everything seems fine. You'll have to add more details if it still isnt.
when you are dealing with arrays the first index is always zero, so if you have a grid that is 10 by 10 your coordinates go from 0 to 9.
Your answer
![](https://koobas.hobune.stream/wayback/20220612194727im_/https://answers.unity.com/themes/thub/images/avi.jpg)