# Instantiating from 2D array, first item is wrong

I’m creating a grid looping through a 2D array. On instantiation I name the grid cells after their X and Y plus I also have two variables in every cell; xCoord and yCoord.

``````private GameObject[, ] cell;
private int height = 100;
private int width = 100;

for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
cell[x, y] = Instantiate(cellPrefab, etc... ;
cell[x, y].gameObject.name = "Cell ( X: " + x + " , Y: " + y + ")";
cellScript.xCoord = x;
cellScript.yCoord = y;
}
}
``````

The name part works - I can click on a cell to get the name and see the cell’s X and Y correctly. However, the xCoord and yCoord variables are not correct - the first cell created should have have xCoord = 0 and yCoord = 0. However they are both 99. Furthermore the cell to the right of the first one should have x = 1 and y = 0, however, it’s 0 and 0. An additional step to the right should be x = 2 and y = 0, but x incorrectly is 1. Y is correct, it’s 0.

Clicking on different cells I see that Y is always correct, but X is always 1 less than it should be.
I can change that by changing the x variable in the instantiate loop from 0 to 1:

``````for (int x = 1; x < width; x++)
``````

This feels wrong/incorrect, plus the first cell still remains xCoord = 99 and yCoord = 99.

I’m so confused. Thank you for any help.

You need to show the real code otherwise how are we supposed to find the problem?

My first instinct was to tell you that “GameObject[, ] cell;” doesn’t produce an array so I guess you skipped it.

You don’t show how “cellScript” relates to that cell either or what it is etc.

Is “cellScript” on the prefab? If so, then you need to get the prefab instance you created (stored in “cell[x, y]”) and do a “GetComponent()” on it (or whatever its component type name is).

1 Like

What is cellScript?

You’re certainly not setting it within the loop.

Did you just leave it pointing perhaps at the original prefab?

Are you getting the first, and last cells mixed up in your head?
The first cell should definitely be Cell (0, 0) not Cell (99, 99)

I tried to shorten it to make it easier to overview.

Here is Grid.cs:

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

public class Grid : MonoBehaviour
{
private int height = 100;
private int width = 100;
float cellSize = 4f;

[SerializeField] GameObject cellPrefab;
private GameObject[, ] cell;

public static List<Cell> structures = new List<Cell>();

void Start()
{
cell = new GameObject[height, width];

Cell cellScript = cellPrefab.GetComponent<Cell>();

for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
cell[x, y] = Instantiate(cellPrefab, new Vector3(x * cellSize, y * cellSize, 0), Quaternion.identity, this.transform);
cell[x, y].gameObject.name = "Cell ( X: " + x + " , Y: " + y + ")";
cellScript.xCoord = x;
cellScript.yCoord = y;

}
}

Quaternion rotation = Quaternion.Euler(90, 0, 0);
transform.rotation = rotation;
}
}
``````

Here is Cell.cs:

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

public class Cell : MonoBehaviour
{
public GameObject structureInThisCell = null;
public bool isOccupied = false;
public int xCoord;
public int yCoord;
}
``````

cellScript is the component on the prefab, not the prefab instances you’re creating! Read what I said above.

``````var myCell = cell[x, y].GetComponent<Cell>();
myCell.xCoord = x;
myCell.yCoord = y;
``````
3 Likes

Great, it works now, thank you.

2 Likes