- Home /

# Jagged Array returns null anywhere outside function that populates it

I have a script that counts the number of tagged GameObjects in the scene and puts them in an array based on their x and z co-ordinates (there are different amounts for each 'row' giving me a jagged array).

At the end of t$$anonymous$$s function I can check if hexArray is null and it says it isn't. If I check t$$anonymous$$s (in Start()) immediately afterwards, it says it is null and even if I call a function from wit$$anonymous$$n PutHexInArray(), it still returns null. So basically anywhere outside of t$$anonymous$$s function, it returns null. I have a feeling it must be a syntax error of some kind, but I can't see it.

I'll cut out the bits of the function that don't relate necessarily:

```
public GameObject[][] hexArray;
private float hexZGap = 10.5f;
private float hexXGap = 12f;
void PutHexInArray()
{
//put all the hexes in a temporary 1d array...
GameObject[] tempHexArray = GameObject.FindGameObjectsWithTag("Hex");
print("t$$anonymous$$s many hexes: " + tempHexArray.Length);
float $$anonymous$$ghestZ = -50000;
float lowestZ = 50000;
for (int i = 0; i < tempHexArray.Length; i++)
{
//...(taken out) I check the $$anonymous$$ghest and lowest Z co-ords for all hexes and use that to
// work out how many rows...
}
int numOfRows = Mathf.RoundToInt(($$anonymous$$ghestZ - lowestZ) / hexZGap) + 1;
//i now know the order of the rows and what their z will be.
//now that we know the number of rows, we know how big to make our array. we can then go
through and write them in order.
//first set our array to the correct length.
GameObject[][] hexArray = new GameObject[numOfRows][];
print("got length: " + hexArray.Length);
//by starting at the lowestZ, and incrementing by hexZGap, we can put them all in the correct
row.
//before we do that, we need to know how many in that row, w$$anonymous$$ch we can do by working out the
$$anonymous$$ghest and lowest X values
//go through row by row first and foremost...
for (int rowNum = 0; rowNum < numOfRows; rowNum++)
{
//(taken out) I do the same t$$anonymous$$ng here as earlier
// I work out the $$anonymous$$ghestX and lowestX to work out how many in the row with the right Z co-ords
}
//now that we have the $$anonymous$$ghest and lowest for that row, we can work out how many in that
row.
int numInRow = Mathf.RoundToInt(($$anonymous$$ghestX - lowestX) / hexXGap) + 1;
print("numInRow: " + numInRow);
//now set t$$anonymous$$s row of the array to the correct length
hexArray[rowNum] = new GameObject[numInRow];
//we can then grab them by using their position.....hopefully!
//so we know how many in each row and the exact position of each now.
for (int i = 0; i < numInRow; i++)
{
float checkForX = (i * hexXGap) + lowestX;
//we know the z in t$$anonymous$$s loop already, and we work out the x as we go!
for (int b = 0; b < tempHexArray.Length; b++)
{
GameObject t$$anonymous$$sHex = tempHexArray[b];
if (t$$anonymous$$sHex.transform.position.z == checkForZ && t$$anonymous$$sHex.transform.position.x == checkForX)
{
hexArray[rowNum][i] = t$$anonymous$$sHex;
}
}
}
}
if (hexArray != null)
{
print("it's not null at the end of PutHexInArray()");
print("in fact it's length is t$$anonymous$$s: " + hexArray.Length);
}
}'
```

The key lines are obviously:

```
GameObject[][] hexArray = new GameObject[numOfRows][];
hexArray[rowNum] = new GameObject[numInRow];
hexArray[rowNum][i] = t$$anonymous$$sHex;
```

Any help would be really appreciated.

**Answer** by lulitd
·
Aug 08, 2017 at 03:05 PM

@hjenkinz Your error is at line 25:

```
GameObject[][] hexArray = new GameObject[numOfRows][];
```

it should be t$$anonymous$$s instead:

hexArray = new GameObject[numOfRows][];

so that you are referring to the hexArray field instead of an instance variable.

Ah you legend, I knew it was a simple syntax error. Thanks muchly!

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

### Follow this Question

### Related Questions

Null error on 2D array only after instantiation 1 Answer

How to add an array to monobehaviour 1 Answer

How do i give Coordinates (x, y) to an Array 1 Answer

How to generate a random int that does not repeat consecutively? 1 Answer

When attempting to serialize and save a jagged array it gives me an error message 1 Answer