How do you store instantiated tiles using a coordinate system?

Hi community. I’m trying to produce a program that generates a gridmap, where each instantiated tile has a script attached generating data unique to each tile. I’m struggling to understand how I can define the name of these instantiated tiles using a coordinate system, shown in the code below. I was attempting to use lists with a row and column value, though it appears lists cannot be used as variable names.


The code (The segment between the two for loops is place where this needs to happen):

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

public class GridGeneration : MonoBehaviour
{
    public int rows = 10;
    public int cols = 10;
    public float tileSize = 5f;
        
    void Start()
    {
        generateGrid();
    }

    void generateGrid()
    {
        GameObject referenceTile = (GameObject)Instantiate(Resources.Load("blankTile"));
        for (int row = 0; row < rows; row++)
        {
            for (int col = 0; col < cols; col++)
            {
                List<int> tileCoordinates = new List<int> { row, col };
                GameObject tile = (GameObject)Instantiate(referenceTile, transform);
                tile.AddComponent<tileData>();
                Debug.Log(tile);
                float posX = col * tileSize;
                float posY = row * tileSize;
            }
        }
        Destroy(referenceTile);

        float gridWidth = cols * tileSize;
        float gridHeight = rows * tileSize;
        transform.position = new Vector2((gridWidth - tileSize) / -2, (gridHeight - tileSize) / 2);
    }
}

Thanks for your time!

Hi. You can use GameObject[,] to store the gameobject in a coordinate-like way.

GameObject[,] tiles;
int maxX;
int maxY;
    
void Start()
{
    tiles = new GameObject[maxX, maxY];
}
    
for (int x = 0; x < maxX; x++)
{
    for(int y = 0; y < maxY; y++)
   {
       GameObejct tileObject = Instantiate(tile, somePositionIDK, someRotationIDK);
       tiles[x, y] = tileObject;
   }
}

You could store the Tiles in a Dictionary. Use a string as a key.

Dictionary<string, GameObject> tiles = new Dictionary<string, GameObject>();    

string key = "r" + row.ToString() + "c" + col.ToString;

tiles[key] = rawTile;

You can now get a tile back by generating a key knowing the row,col of the tile you need. The key could also be stored as the tile name in the TileData script.
Another note. You could attach the TileData script to the perfab blankTile so it’s there when you use it to instantiate.