Keeping track of array objects

I’m making a game where i need to create a 68x68 grid of cubes that are invisible but are set as triggers. I already have the prefab and i know i need a array, But i can’t seem to figure out how to keep track of every object in the array so i can access it later. Like maybe number each object differently. Thanks in advance.

There are 2 ways to do it.

one by naming each cube along with the x and y indices.

other by making use of the fact that it is a grid and calculate the distance between the first grid and this to calculate the index.

Let me explain the first method.

When you create the grid, name the objects like below

cube_0_0
cube_0_1
.
.
.
.
cube_67_66
cube_67_67

And in the code you can find the index by splitting the name of the object.

void OnTriggerEnter(Collider other)
{

   string[] splitString  = other.name.Split("_"[0]);
   //now at index zero the name cube will be there and in 1 and 2 it will be the index on x and y

  int indexX= System.Int32.Parse(splitString[1]);
  int indexY= System.Int32.Parse(splitString[2]);

  // do blah blah blah with indices...

}

I usually dont prefer this method, because it takes lot of time to setup and also it is not a very feasible method, when lot of changes involve in code.

And the second method by using the distance…

Since the distance between any two adjacent objects in the same direction is always equal, you can find the distance between 2 the very first object of the grid and this and with this you can easily calculate the index of the object.

void OnTriggerEnter(Collider other)
{

 Vector3 _distance = other.transform.position - firstObjectOfGrid.position; 

//firstObjectOfGrid could be linked to the editor or found using tag.

 int indexX = (int)(_distance.x/GridSizeOnX);   //GridSizeOnX is the constant size of grid on X, simply difference between any adjacent objects on X.
 int indexY = (int)(_distance.x/GridSizeOnY);

}

Note that in the manager script, you can do the same thing for adding to the array
.

public GameObject[,] allObjects

void Awake()
{
      //make sure to tag all objects as gridObject or similar..
      
      allObjects = new GameObject[68,68];

      GameObject[] tempArray = GameObject.FindObjectsWithTag("gridObject");

     for(int i=0;i<68;i++)           // it is not a good practice to use number straight away, declare a variable for this and do so that changes could be easily done.
     {
           for(int j=0;j<68;j++)
           {
                Vector3 _distance = other.transform.position - firstObjectOfGrid.position; 
                int indexX = (int)(_distance.x/GridSizeOnX);  
                int indexY = (int)(_distance.x/GridSizeOnY);
           }
      }

}

The second method is convenient in lot of ways but one draw back is, if the girds are not equally spaced.

the second method will be more convenient if the OnTriggerEnter is check on the Object and not player, because you can calculate its index in start and save and then when collision is happening you can just check if the collided object is the current object and do the calculation. The below is the sample…

public class GridObject : Monobehaviour
{
    public int indexX;
    public int indexY;

    void Start()
    {   

       Vector3 _distance = transform.position - Gameobject.Find("FirstGridObjet");.transform.position;   

       indexX = (int)(_distance.x/GridSizeOnX);  
       indexY = (int)(_distance.x/GridSizeOnY);
             
    } 

     void OnTriggerEnter(Collider other)
     {
           if(other.gameObject == gameObject)
           {

                 //send message to the manager or similar and do blah blah blah...
           }
     }

}

Sounds like a 2D array:

// assign this one in the editor to your cube prefab:
public Transform prefab;
// Not sure about the brackets, afaik they belong there:
private Transform[,] gridArray = new Transform[68,68]();

public void initTriggerGrid()
{
    for (int i = 0; i < 68; i++)
    {
        for (int j = 0; j < 68; j++)
        {
            gridArray[i,j] = Instantiate(prefab, new Vector3(i*1.0f, 0, j*1.0f), Quaternion.identity) as Transform;
        }
    }
}