- Home /
Filling a board with tiles, with separation
I'm creating a basic prototype of something I have in mind, a sort of board game. Here's a picture if you're interested: http://i.imgur.com/WVck6MY.png
Each of these tiles is a separate game object right now just to show the setup. I know I can use two for-loops to place a single tile multiple times onto the board instead of having tons of identical separate game objects, but I have trouble wrapping my head around how to create 'gaps' between each tile so they're not all squished together (check my picture and you'll see there is a noticeable gap between each tile). Any help on how to generate this via code is appreciated.
I have a GameManager empty object with the following script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameManager : MonoBehaviour {
public GameObject TilePrefab;
List <List<Tile>> board = new List <List<Tile>>();
public int maxRows = 4;
public int maxCols = 5;
// Use this for initialization
void Start () {
generateBoard ();
}
// Update is called once per frame
void Update () {
}
void generateBoard ()
{
board = new List<List<Tile>> ();
for (int i = 0; i < maxRows; i++)
{
List <Tile> row = new List<Tile>();
for (int j = 0; j < maxCols; j++)
{
Tile tile = ((GameObject)Instantiate(TilePrefab,
new Vector3(i - Mathf.Floor(maxRows/2),0, -j + Mathf.Floor (maxCols/2)),
new Quaternion(new Vector3()))).GetComponent<Tile>();
tile.boardPosition = Vector2(i,j);
row.Add (tile);
}
board.Add (row);
}
}
}
The tile class is basically empty, except for the default statements Unity provides and this line:
public Vector2 boardPosition = Vector2.zero;
The easiest way to solve your problem is to leave your code the way it is and reduce the size of your prefab. Placing your objects on integer positions can simplify other code, so if at all possible, leave your code alone.