var newObstacle = Instantiate(cube, new Vector3(i, 0, j), Quaternion.identity, transform);
Renderer obstacleRenderer = newObstacle.GetComponent<Renderer>();
Material mat = new Material(obstacleRenderer.sharedMaterial);
mat.color = GetRandomColor();
obstacleRenderer.sharedMaterial = mat;
// for reference methods
public Color GetRandomColor()
{
return new Color(Random.Range(0, 255),Random.Range(0, 255),Random.Range(0, 255));
}
// for specific
public Color GetRandomColor()
{
var random = Random.Range(1, 10);
if(random<4) return Color.red;
else if(random<7) return Color.blue;
else return Color.green;
}