- Home /
Question by
Brandon194 · Jun 27, 2020 at 11:50 AM ·
positioninginstantiate-objects
Code generating 2 different results?
Not sure why, its working when I use one set of variables, but not the other.
Screenshots of results and code: https://imgur.com/a/fVJTaEr
void Start()
{
board = GetComponentInChildren<Transform>().gameObject;
int[] sizeSpaces = { Mathf.FloorToInt(boardSize.x), Mathf.FloorToInt(boardSize.y) };
boardPieces = new GameObject[sizeSpaces[0],sizeSpaces[1]];
for (int j = 0; j < boardSize.y; j=j+1)
{
for(int i = 0; i < boardSize.x; i=i+1)
{
Debug.Log("" + i + "," + j);
boardPieces[i,j] = GameObject.Instantiate(
boardPiece,
new Vector3(
(-boardSize.x / 2) + i +.5f,
0.15f, (-boardSize.x / 2) + j + .5f),
new Quaternion(0,0,0,0),
this.transform);
}
}
}
Comment
Best Answer
Answer by Brandon194 · Jun 27, 2020 at 12:41 PM
Issue solved.
void Start()
{
board = GetComponentInChildren<Transform>().gameObject;
sizeSpaces = new int[]{ Mathf.FloorToInt(boardSize.x), Mathf.FloorToInt(boardSize.y) };
boardPieces = new GameObject[sizeSpaces[0],sizeSpaces[1]];
for (int j = 0; j < boardSize.y; j=j+1)
{
for(int i = 0; i < boardSize.x; i=i+1)
{
Debug.Log("" + i + "," + j);
boardPieces[i,j] = GameObject.Instantiate(boardPiece, this.transform);
boardPieces[i, j].transform.localPosition = new Vector3(Mathf.Ceil(-boardSize.x / 2 + i), .15f, Mathf.Ceil(-boardSize.y / 2 + j));
}
}
}