Why when i color the 4 walls or getting all the blocks from the 4 walls they are not equal ?

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 ?

In the first script, from lines 83 to 108 you are only coloring one color, then abandoning the other possibilities that are the same, i.e., the if-else constructs.

ALSO, you should not be testing Vector3 objects for equality. Floating point numbers can seem to be identical but their equality will fail due to floating point precision errors. Instead check the .magnitude between them and see if they are below a certain amount, such as Mathf.Epsilon, a small constant designed for this purpose.

In the second script you are finding 36 walls because that’s how many walls are around the edge of your 10x10 square. Nowhere in your code did you make “double blocks” in the corner, at least that I can see, so it won’t be 40.

1 Like

That is what i tried:

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>();
    public 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();
    }

    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);
               
                if (block.transform.localPosition.x == 0)//TOP
                {
                    positions.Add(block.transform.localPosition);
                    block.transform.name = "TopWall";
                    block.GetComponent<Renderer>().material.color = Color.red;
                }
                else if (block.transform.localPosition.z == 0 || 
                    (block.transform.localPosition.z == 0 &&
                    block.transform.localPosition.x == 0))//LEFT
                {
                    positions.Add(block.transform.localPosition);
                    block.transform.name = "LeftWall";
                    block.GetComponent<Renderer>().material.color = Color.blue;
                }
                else if (block.transform.localPosition.z == gridWidth - 1)//RIGHT
                {
                    positions.Add(block.transform.localPosition);
                    block.transform.name = "RightWall";
                    block.GetComponent<Renderer>().material.color = Color.green;
                }

                else if (block.transform.localPosition.x == gridHeight - 1)//BOTTOM
                {
                    positions.Add(block.transform.localPosition);
                    block.transform.name = "BottomWall";
                    block.GetComponent<Renderer>().material.color = Color.yellow;
                }

                blocks.Add(block);
            }
        }
    }
}

I tried first to change the LEFT wall Else/If condition:

else if (block.transform.localPosition.z == 0 || 
                    (block.transform.localPosition.z == 0 &&
                    block.transform.localPosition.x == 0))

I added that OR if z and x both = 0 but this condition will never happen so the left wall start when X is already at 1
And i want the overlap i want that every color will color 10 blocks so first i tried with the left wall but it’s wrong.