# How can I select random two positions from the grid outer square and also that the randoms will be from two walls facing each other?

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";
gridBlocks[x, z] = block.transform;
if ((x == 0) || (x == gridWidth - 1) || (z == 0) || (z == gridHeight - 1))
{

var cubeRenderer = block.GetComponent<Renderer>();
cubeRenderer.material.SetColor("_Color", Color.red);
}
}
}

gridGenerated = true;
}


Here I color the outer grid blocks in red. But now I want to color in blue two positions each time and each one from the other outer side.

So each time I'm running the game it should pick two random positions each one from the parallel sides : For example :

Or like this :

So the two random positions will be picked from two parallel sides.

This is what I tried :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenerateGrid : MonoBehaviour
{
public GameObject gridPrefab;
public GameObject pathPrefab;

private Transform[,] gridBlocks;

private List<Vector3> gridBlocksPositions = new List<Vector3>();

private List<Transform> gridOuterBlocks = new List<Transform>();
private List<Vector3> gridOuterBlocksX = new List<Vector3>();
private List<Vector3> gridOuterBlocksXLength = new List<Vector3>();
private List<Vector3> gridOuterBlocksZ = new List<Vector3>();
private List<Vector3> gridOuterBlocksZLength = new List<Vector3>();

private Vector3 startBlockPOs;
private Vector3 endBlockPos;

public int gridWidth = 10;
public int gridHeight = 10;
public int gap;
public float spawnSpeed = 0;
public bool gridGenerated = false;

void Start()
{
StartCoroutine(CreateGrid());
}

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";
gridBlocks[x, z] = block.transform;
if ((x == 0) || (x == gridWidth - 1) || (z == 0) || (z == gridHeight - 1))
{
if(x == 0)
{
}

if(x == gridWidth - 1)
{
}

if(z == 0)
{
}

if(z == gridHeight - 1)
{
}

var blockRenderer = block.GetComponent<Renderer>();
blockRenderer.material.SetColor("_Color", Color.red);
}
}
}

PickRandom();

gridGenerated = true;
}

private void PickRandom()
{
Transform randBlock = gridOuterBlocks[Random.Range(0, gridOuterBlocks.Count)];

if(randBlock.position.x == 0)
{
startBlockPOs = randBlock.position;
}

if (randBlock.position.x == gridWidth - 1)
{
endBlockPos = gridOuterBlocksXLength[Random.Range(0, gridOuterBlocksXLength.Count)];
}

if (randBlock.position.z == 0)
{
startBlockPOs = randBlock.position;
}

if (randBlock.position.z == gridHeight - 1)
{
endBlockPos = gridOuterBlocksZLength[Random.Range(0, gridOuterBlocksZLength.Count)];
}

for(int i = 0; i < gridOuterBlocks.Count; i++)
{
if(startBlockPOs == gridOuterBlocks[i].position ||
endBlockPos == gridOuterBlocks[i].position)
{
var rend = gridOuterBlocks[i].GetComponent<Renderer>();
rend.material.SetColor("_Color", Color.blue);
}
}
}
}


But the result is all the time or 99% of the time that he pick the start position the 0,0 top left corner block and then the end position also at the top left corner the left example in the screenshot or he pick two positions start and end on same side the right example in the screenshot :

I can't make it to pick a random positions like I wanted in the two top screenshots examples with the light blue color.

The rules should be :

1. Never on the same side.

2. Never on the same position.

3. Always to pick the end position from the parallel wall side.

You could add inside the CreateGrid():

    if (Random.Range(0,2)==0){
var blockRenderer=GridBlocks[0,Random.Range(0,GridHeight)].GetComponent<Renderer>();
blockRenderer.material.SetColor("_Color", Color.blue);
blockRenderer = GridBlocks[GridWidth-1,Random.Range(0,GridHeight)].GetComponent<Renderer>();
blockRenderer.material.SetColor("_Color", Color.blue);
}else{
var blockRenderer =GridBlocks[Random.Range(0,GridWidth),0].GetComponent<Renderer>();
blockRenderer.material.SetColor("_Color", Color.blue);
blockRenderer = GridBlocks[Random.Range(0,GridWidth),GridHeight-1].GetComponent<Renderer>();
blockRenderer.material.SetColor("_Color", Color.blue);
}


This method mostly utilizes the fact that you already have all elements of grid with their positions in Transform[,] gridBlocks. So using it with help of Random.Range(0,2) we decides will we recolor vertical or horizontal borders of the grid, afterwards just randomizing which to choose from both sides.

It would be like that in your code:

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";
gridBlocks[x, z] = block.transform;
if ((x == 0) || (x == gridWidth - 1) || (z == 0) || (z == gridHeight - 1))
{

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);
blockRenderer = GridBlocks[GridWidth-1,Random.Range(0,GridHeight)].GetComponent<Renderer>();
blockRenderer.material.SetColor("_Color", Color.blue);
}else{
var blockRenderer =GridBlocks[Random.Range(0,GridWidth),0].GetComponent<Renderer>();
blockRenderer.material.SetColor("_Color", Color.blue);
blockRenderer = GridBlocks[Random.Range(0,GridWidth),GridHeight-1].GetComponent<Renderer>();
blockRenderer.material.SetColor("_Color", Color.blue);
}
gridGenerated = true;
}