[Solved] How an I find grid outer square positions by formula by script?

user1196715 Asks: How an I find grid outer square positions by formula by script?
I mean automatic to find the outer grid square :

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenerateGrid : MonoBehaviour
{
    public GameObject gridPrefab;

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

        gridGenerated = true;
    }
}

I marked them selected them in the screenshot to show what I mean by the outer square. I want to find automatic by some code logic only the outer square of the grid no matter what is the grid size if it’s 10 on 10 or 3 on 7


Grid

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.