var terrainArray: int[,] = new int[gridRows, gridCols];
I want to be able to add an additional Row (or Column) to this Array at will, and populate it. How would I go about doing this?
Why?: I’m making an infinite tile-based Terrain generator, and as the player approaches the edge of the grid, a new row or column is made and generated on the fly.
You can’t resize a 2D array. You can make a new array with a different size and copy the contents over, but that’s not really a good idea. It would be better to just keep the array the same size, and move content around as needed. So when the player approaches an edge, shift the contents so that the old stuff on the opposite edge is removed to make room for the new stuff.
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
// Declare 2d array
public int [,] Tablica;
void Start () {
// Set size 128x128
Tablica = new int[128,128];
// Write into array position 115,5 / value 987
Tablica[115,5] = 987;
// Read from array position 115,5 and print it as string
print (Tablica[115,5].ToString());
}
void Update () {
}
}