Array seemingly accessing only last value

Premise:
I have this code that I’m using to save game data. The data being saved is a 2D array of chunks, in which each chunk contains 2 20x20 2D arrays, 2 for each tile in the chunk (for terrain data and object data on the tile). In the save file, first the map’s dimensions are printed, then for each chunk the terrain then object data are printed. Chunks are saved with whitespace between the two (so each chunk is saved as a 20x40 block of integers.

Problem:
However when I save it only saves the very last chunk. So if I have a 5x5 array of chunks then it only saves the one at index [4, 4]. If a 2x5 array then only at [1, 4]. It does save the chunks the “proper amount” of times however, so if 5x5 then chunk [4, 4] will be saved 25 times.

Code:

    public void SaveGameData()
    {
        //Game Map Manager
        MapManager manager = gameObject.GetComponent<MapManager>();

        //Name of the Save File
        string fileName = saveName + ".txt";

        //Write Data to the Save File
        StreamWriter writer = new StreamWriter(fileName, false);

        //Get Data of each Chunk
        int x = manager.chunkArray.GetLength(0);
        int y = manager.chunkArray.GetLength(1);

        //Get Number of Chunks
        writer.WriteLine("Chunk Size: " + x + " " + y);

        for (int i = 0; i < x; i++)
        {
            for (int j = 0; j < y; j++)
            {
                //Get the Chunk
                GameObject saveChunk = manager.chunkArray[i, j];
                Chunk chunkData = saveChunk.GetComponent<Chunk>();

                int[,] terrainData = chunkData.terrainIDs;
                int[,] objectData = chunkData.objectIDs;

                //Iterate and Store the Terrain IDs
                for (int k = 0; k < 20; k++)
                {
                    //Break Between Sections
                    writer.WriteLine();
                    for (int l = 0; l < 20; l++)
                    {
                        //Store the Data
                        writer.Write(terrainData[k, l] + " ");
                    }
                }

                //Iterate and Store the Object IDs
                for (int k = 0; k < 20; k++)
                {
                    //Break Between Sections
                    writer.WriteLine();
                    for (int l = 0; l < 20; l++)
                    {
                        //Store the Data
                        writer.Write(objectData[k, l] + " ");
                    }
                }

                //Break between Chunks
                writer.WriteLine();
            }
        }

        writer.Close();
    }

You’ve verified that on line 25 of your code, the chunkData contains the values your’e expecting? No chance you have multiple Chunk components on it, and it’s just returning one of them?

Yes, through the inspector I can see that there’s only one Chunk component on it. I can confirm their values are correct through a 1D array that shows the values of the last row of its objectIDs array. I can also see that each chunk is being properly accessed. Before I posted this code, I had a line immediately after line 25 that Debug.Log the name of chunkData’s gameobject, which came out correct.

I do have this script that’s being used to create the chunks, could it be causing a problem?

    public void GenerateMap(int x, int y)
    {
        //The Amount of Chunks and Their Population Data
        chunkArray = new GameObject[x, y];
        terrainIdArray = new int[20,20];
        objectIdArray = new int[20,20];

        for (int i = 0; i < x; i++)
        {
            for (int j = 0; j < y; j++)
            {
                //Fill Tile Data for each Chunk
                for (int k = 0; k < 20; k++)
                {
                    for (int l = 0; l < 20; l++)
                    {
                        terrainIdArray[k, l] = 1;
                        objectIdArray[k, l] = Random.Range(0, 2);                     
                    }
                }

                //Instantiate and Populate Chunks
                chunkArray[i, j] = new GameObject("Chunk" + i + "-" + j);
                chunkArray[i, j].transform.parent = Map.transform;
                chunkArray[i, j].transform.localPosition = new Vector3((20 * i), 0f, (20 * j));
                chunkArray[i, j].AddComponent<Chunk>();

                Chunk chunkController = chunkArray[i, j].GetComponent<Chunk>();
                chunkController.terrainIDs = terrainIdArray;
                chunkController.objectIDs = objectIdArray;
                chunkController.gameManager = gameObject;
                chunkController.GenerateChunk();
            }
        }
    }

On lines 5 and 6 you create two 2D arrays.

On lines 29 and 30, you associate each chunk with these same two arrays. So from then on, if you change their values through any of the chunks, you would be changing it for all the chunks.

Try this instead:

chunkController.terrainIDs = new int[20,20];
chunkController.objectIDs = new int[20,20];

Thank you so much! That took care of the problem.

1 Like