Why when i color 4 walls of a grid 10x10 it's coloring 10,9,9,8 and not 10,10,10,10 ?
I'm sure the problem is somewhere with the IF/ELSE conditions and maybe i should use magnitude and math.epsilon but not sure how and how to figure it logic why it's not coloring all the 4 walls.
In the end i'm getting 36 colored blocks instead 40.
This is a screenshot showing on the scene view the grid and the 4 walls of what i mean colored in red,blue,green,yellow
I know that if i color them all they will meet at the 4 corners at 0,0 and 0,9 and 9,0 and 9,9 But still i want to color them all and get 40 colored blocks and not 36.
Now the way i'm coloring it i'm getting this: Red = 10 colored blocks Blue and Green both = 9 colored blocks and Yellow = 8 colored blocks. I wonder how can i use magnitude and math.epsilon if needed and how and what to change in the IF/ELSE statements ?
And this is the script:
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.parent = wallsParents[0].transform;
block.transform.name = "TopWall";
block.GetComponent<Renderer>().material.color = Color.red;
}
else if (block.transform.localPosition.z == 0)//LEFT
{
positions.Add(block.transform.localPosition);
block.transform.parent = wallsParents[1].transform;
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.parent = wallsParents[2].transform;
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.parent = wallsParents[3].transform;
block.transform.name = "BottomWall";
block.GetComponent<Renderer>().material.color = Color.yellow;
}
blocks.Add(block);
}
}
}
}
Looks like you want the length of each side to be 11 blocks, not 10. This way, each side including the corner square can be 10 blocks, without overlapping the corner of the "next" side.
How can i do it ? And then if i will want to get all this 40 colored blocks if the length is 11 i will get 40 blocks ? 4 walls 10 in each one should be 40. I guess i didn't understand yet. If you could show me please.
public int gridWidth = 11;
public int gridHeight = 11;
The issue is the overlap of the corner blocks. This is why you only see 36 = (4*10)blocks $$anonymous$$us 4 overlaps =36 colored blocks. So, if you use 11 for each side you get (11*4) blocks $$anonymous$$us 4 overlapping blocks = 40 colored blocks.
Your answer
Follow this Question
Related Questions
I create material with script but it does not render right 0 Answers
Creating Splines from empties in script 0 Answers
How can i rotate all the child objects together at the same time ? 1 Answer
How can i give another name/number to the created Plane object name ? 0 Answers
How can i create List of maps from each Map class ? 0 Answers