Creating a checker board of cubes with two different colors.

HI, I’m trying to make a grid of cubes 25x25 which I have, but I’d like every other cube to have a different color, like a checker board. This is the code I have so far which at least creates a floor of cubes:

using UnityEngine;
using System.Collections;

public class MainScript : MonoBehaviour {
	public Transform cube;
	public int gridWidth=25;
	public int gridDepth=25;

	//public int gridHeight=10;
	void Start() 

		//for (int y = 0; y < gridHeight; y=y+2) 
		for (int z = 0; z < gridDepth; z=z+1) {
			for (int x = 0; x < gridWidth; x = x +1) {
				Instantiate (cube, new Vector3 (x, 0, z), Quaternion.identity);






Thanks in advance. :slight_smile:

Create two materials and assign them to script’s public Material

public int gridWidth=25;
public int gridDepth=25;
public Material[] materalList = new Material[2];
int matIndex = 0;

Instantiate function returns the newly created gameobject. So basically, you can change the material of the object.

GameObject _clone = Instantiate (cube, new Vector3 (x, 0, z), Quaternion.identity) as GameObject;

_clone.GetComponent<Renderer>().material = materialList[matIndex];
matIndex = (matIndex == 0) ? 1 : 0;

you can make two prefabs(black cube and white cube).
so make two prefabs with two different materials(colors) and use them consecutively in your code(loop).