- Home /
Why when i color the 4 walls or getting all the blocks from the 4 walls they are not equal ?
How can i color the grid 4 walls and add spaces between cubes ? This is my script now.
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 positions = new List();
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;
}
}
}
}
}
}
I have some problems:
All the If's else conditions not checking cases where the cubes are meeting that's 4 corners. For example at position 0,0 the top wall and the left wall should meet. Same for position at the top right corner and all corners. The problem is that it's coloring: 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 But i think if i'm not mistaken it should color each wall all the cubes that's 40 cubes. I understand that at the corners the colors will mix but it should color all the walls. But what i'm getting is 36 cubes colored. And it's not coloring equal the walls: 10,9,9,8 Second problem is that i'm doing the comparison wrong i guess for example: if (blocks[x].transform.localPosition == positions[i]) { if (blocks[x].transform.localPosition.x == 0)//TOP I want to use magnitude and math.epsilon to get the closet position but not sure how to do it. When i will want to use another scripts to get the walls blocks by name "TopWall" , "LeftWall" , "RightWall" , "BottomWall" i will get 36 blocks gameobjects and not 40 i should get on each wall 10 blocks gameobjects but i'm getting 10,,9,9,8 The last problem is if i want to make spaces between the cubes in the grid for example: block.transform.localPosition = new Vector3(x 10, 0, z 10); Now it will not color all the cubes since the coordinates now are not the same as before so the whole calculations in ColorWalls method are wrong. This is a screenshot of the grid: You can see the problem with coloring the 4 walls and then when i'm getting this 4 walls and then all the blocks of them i'm getting 36 not 40.