Cannot apply indexing with [] to an expression of type 'GameObject'

I’ve defined a List of gameObjects like so in my Start method

 public List<GameObject> Tiles { get; set; }
        private GameObject _tileInstance;

void Start(){
 Tiles = new List<GameObject>();
            for (int i = 1; i <= 10; i++)
            {
                for (int j = 1; j <= 10; j++)
                {
            int xVal;
                    int zVal;
                    xVal = initX + ((j - 1) * 2);
                    zVal = initZ - ((i - 1) * 2);
                    _tileInstance = Instantiate(tilePrefab, new Vector3(xVal, 0, zVal), Quaternion.Euler(0, 0, 0)) as GameObject;                  
                    Tiles.Add(_tileInstance);
                    _tileInstance.transform.SetParent(tileHolder.transform, false);
                }
            }
}

Then later on I want to access some of these gameObjects like so

 _tileInstance = Tiles[startrow][endcolumn];

But get the error

Why?

I do not understand your question but I’m inclined to answer “For the exact reason stated?”.

List is a list of game objects.

List[int] is a GameObject.

GameObject doesn’t have an indexer.

Do you want a two-dimensional array? Why not just create a two-dimensional array…

GameObject[,] Tiles { get; set;}
//...
Tiles = new GameObject[10, 10];
//...
Tiles[x,y] = /*make a new tile*/

Typically, though, a tile table has only one dimension, so I don’t know what you’re trying to do, here.

EDIT: This doesn’t seem very specific to Unity, either. If this is a “how to write code in C#” question, you can find hundreds of resources on that subject, elsewhere.

@MaxGuernseyIII answered before I could hit submit, and said what I was going to say and a little bit more :slight_smile:

Beyond that, I can only add that it’s possible to do a “similar” idea to list[×][y] with a 1-dimensional array if you use math :slight_smile: multiply and add to arrive at where it would be, that sort of thing, I mean.

Thanks. Tiles represents a 2D game board. so maybe an Array will work better

I’ve occasionally used a 2 dimensional list (not even sure its actually called that) in cases like this instead of a 2D array, mostly just because I like dealing with lists instead. Its a list of list of GameObjects basically. You declare it something like this:
public List<List> Tiles;