Nerd For Tech
Published in

Nerd For Tech

Generate a Game Board in Unity

Objective: Generate a rectangular game board during run time in Unity.

We’ll begin creating our board by defining a few parameters: The two prefabs that will make-up our alternating squares, the number of columns and rows we want, and how fast we’d like to draw each square.

The board is drawn by two loops, with the row loop nested under the column loop. So first a full row will be drawn, then the script will move to the next column.

If a black square is to be placed, the script will instantiate the black square prefab at the proper position from the board’s starting position, set the proper rotation, and make the square a child of the board object. If it should not be black, we’ll instantiate the other square.

After drawing the square, we’ll switch _isBlack (from true to false or vice versa). Then we’ll wait a bit before drawing the next square.

Finally, if the grid has an even number of rows, we’ll switch _isBlack once again. This will ensure the grid alternates colors properly!

The lower board has 30x8 while the higher board is 5x7.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store