At the top :
public List<Text> texts = new List<Text>();
Than when creating a grid :
IEnumerator CreateGrid()
{
gridBlocks = new Transform[gridWidth, gridHeight];
for (int x = 0; x < gridWidth; x++)
{
yield return new WaitForSeconds(spawnSpeed);
for (int z = 0; z < gridHeight; z++)
{
yield return new WaitForSeconds(spawnSpeed);
GameObject block = Instantiate(gridPrefab, Vector3.zero, gridPrefab.transform.rotation) as GameObject;
block.transform.parent = transform;
block.transform.localPosition = new Vector3(x + x * gap, 0, z + z * gap);
block.tag = "Grid Block";
texts.Add(block.GetComponentInChildren<Text>());
gridBlocks[x, z] = block.transform;
gridBlocksPositions.Add(block.transform.position);
if ((x == 0) || (x == gridWidth - 1) || (z == 0) || (z == gridHeight - 1))
{
gridOuterBlocks.Add(block.transform.position);
var cubeRenderer = block.GetComponent<Renderer>();
cubeRenderer.material.SetColor("_Color", Color.red);
}
}
}
if (Random.Range(0, 2) == 0)
{
var blockRenderer = gridBlocks[0, Random.Range(0, gridHeight)].GetComponent<Renderer>();
blockRenderer.material.SetColor("_Color", Color.blue);
startBlockPOs = blockRenderer.transform.position;
blockRenderer = gridBlocks[gridWidth - 1, Random.Range(0, gridHeight)].GetComponent<Renderer>();
blockRenderer.material.SetColor("_Color", Color.blue);
endBlockPos = blockRenderer.transform.position;
}
else
{
var blockRenderer = gridBlocks[Random.Range(0, gridWidth), 0].GetComponent<Renderer>();
blockRenderer.material.SetColor("_Color", Color.blue);
startBlockPOs = blockRenderer.transform.position;
blockRenderer = gridBlocks[Random.Range(0, gridWidth), gridHeight - 1].GetComponent<Renderer>();
blockRenderer.material.SetColor("_Color", Color.blue);
endBlockPos = blockRenderer.transform.position;
}
gridGenerated = true;
Test();
}
Inside the CreateGrid method I'm getting the text from each block :
texts.Add(block.GetComponentInChildren<Text>());
Than inside Test I loop over the texts and writing to each text the x and y of each block. It should show me the text 0,0 than 0,1 then 0,2 and so on... Instead it's showing on all the texts 9,9
The grid in this case height and width are 10.
private void Test()
{
for (int i = 0; i < texts.Count; i++)
{
for (int x = 0; x < gridHeight; x++)
{
for (int y = 0; y < gridWidth; y++)
{
blocksX.Add(x.ToString());
blocksY.Add(y.ToString());
texts[i].text = x.ToString() + ", " + y.ToString();
}
}
}
}