- 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 this function I can check if hexArray is null and it says it isn't. If I check this (in Start()) immediately afterwards, it says it is null and even if I call a function from within PutHexInArray(), it still returns null. So basically anywhere outside of this 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("this many hexes: " + tempHexArray.Length);
float highestZ = -50000;
float lowestZ = 50000;
for (int i = 0; i < tempHexArray.Length; i++)
{
//...(taken out) I check the highest and lowest Z co-ords for all hexes and use that to
// work out how many rows...
}
int numOfRows = Mathf.RoundToInt((highestZ - 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, which we can do by working out the
highest 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 thing here as earlier
// I work out the highestX and lowestX to work out how many in the row with the right Z co-ords
}
//now that we have the highest and lowest for that row, we can work out how many in that
row.
int numInRow = Mathf.RoundToInt((highestX - lowestX) / hexXGap) + 1;
print("numInRow: " + numInRow);
//now set this 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 this loop already, and we work out the x as we go!
for (int b = 0; b < tempHexArray.Length; b++)
{
GameObject thisHex = tempHexArray[b];
if (thisHex.transform.position.z == checkForZ && thisHex.transform.position.x == checkForX)
{
hexArray[rowNum][i] = thisHex;
}
}
}
}
if (hexArray != null)
{
print("it's not null at the end of PutHexInArray()");
print("in fact it's length is this: " + hexArray.Length);
}
}'
The key lines are obviously:
GameObject[][] hexArray = new GameObject[numOfRows][];
hexArray[rowNum] = new GameObject[numInRow];
hexArray[rowNum][i] = thisHex;
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 this 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
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