TopWall count is 10 cubes
LeftWall count is 9 cubes
RightWall count is 9 cubes
BottomWall count is 8 cubes
In the screenshot the top wall in red count 10 cubes
The left and right blue and green count 9
The bottom yellow count 8
I understand you can’t color all the 10 cubes on each wall since they are meeting at the 4 corners so each wall should be colored with 9 cubes ? Or 8 cubes ?
There 4 walls and 40 cubes.
So the first problem is how the colored walls should be ? I guess i did something wrong in the ColorWalls method.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridGenerator : MonoBehaviour
{
public GameObject gridBlock;
public int gridWidth = 10;
public int gridHeight = 10;
public List<Vector3> positions = new List<Vector3>();
private List<GameObject> blocks = new List<GameObject>();
private GameObject[] wallsParents = new GameObject[4];
void Start()
{
wallsParents[0] = GameObject.Find("Top Wall");
wallsParents[1] = GameObject.Find("Left Wall");
wallsParents[2] = GameObject.Find("Right Wall");
wallsParents[3] = GameObject.Find("Bottom Wall");
GenerateGrid();
GetPositions();
ColorWalls();
}
private void GenerateGrid()
{
for (int x = 0; x < gridWidth; x++)
{
for (int z = 0; z < gridHeight; z++)
{
GameObject block = Instantiate(gridBlock, Vector3.zero, gridBlock.transform.rotation) as GameObject;
block.transform.parent = transform;
block.transform.tag = "Block";
block.transform.localPosition = new Vector3(x, 0, z);
blocks.Add(block);
}
}
}
private void GetPositions()
{
// Top wall
for (int i = 0; i < gridWidth; i++)
{
positions.Add(new Vector3(0, 0, i));
}
// Left wall
for (int i = 0; i < gridHeight; i++)
{
positions.Add(new Vector3(i, 0, 0));
}
// Right wall
for (int i = 0; i < gridHeight; i++)
{
positions.Add(new Vector3(i, 0, gridWidth - 1));
}
// Bottom wall
for (int i = 0; i < gridWidth; i++)
{
positions.Add(new Vector3(gridHeight - 1, 0, i));
}
}
private void ColorWalls()
{
for (int i = 0; i < positions.Count; i++)
{
for (int x = 0; x < blocks.Count; x++)
{
if (blocks[x].transform.localPosition == positions[i])
{
if (blocks[x].transform.localPosition.x == 0)//TOP
{
blocks[x].transform.parent = wallsParents[0].transform;
blocks[x].transform.name = "TopWall";
blocks[x].GetComponent<Renderer>().material.color = Color.red;
}
else if (blocks[x].transform.localPosition.z == 0)//LEFT
{
blocks[x].transform.parent = wallsParents[1].transform;
blocks[x].transform.name = "LeftWall";
blocks[x].GetComponent<Renderer>().material.color = Color.blue;
}
else if (blocks[x].transform.localPosition.z == gridWidth - 1)//RIGHT
{
blocks[x].transform.parent = wallsParents[2].transform;
blocks[x].transform.name = "RightWall";
blocks[x].GetComponent<Renderer>().material.color = Color.green;
}
else if (blocks[x].transform.localPosition.x == gridHeight - 1)//BOTTOM
{
blocks[x].transform.parent = wallsParents[3].transform;
blocks[x].transform.name = "BottomWall";
blocks[x].GetComponent<Renderer>().material.color = Color.yellow;
}
}
}
}
}
}
Then on the a second script i’m trying to get all the 40 gameobjects of the 4 walls.
But i’m getting only 36:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class Test : MonoBehaviour
{
private List<GameObject> blocks = new List<GameObject>();
// Use this for initialization
void Start ()
{
GameObject wall = GameObject.Find("Walls");
var walls = wall.transform.Cast<Transform>().ToList().ConvertAll<GameObject>(t => t.gameObject);
for (int i = 0; i < walls.Count; i++)
{
blocks = walls[i].transform.Cast<Transform>().ToList().ConvertAll<GameObject>(t => t.gameObject);
}
}
// Update is called once per frame
void Update ()
{
}
}
It’s fine to get 36 cubes since i don’t want to use the 4 corners of the grid since some cubes have the same position anyway so i only need the 36 cubes.
I just wonder about the colors problem and what if i wanted to get the whole 40 cubes of the 4 walls ?
