Hello, I have a small problem.
I am trying to delete obejcts from a list.
What I am doing is creating a hexagonal grid, each hexagonal grid gets added to the list.After the list is full,Random objects from the list are chosen and destroyed , whoever when object gets destroyed its place in the list get replaced with → Missing(Gameobject), My question is how could I remove this reference to the object so that only existing objects remain in the list ?
Here is my code.
public class MapGenerator : MonoBehaviour
{
public int mapSize;
public GameObject landType_WATER;
public GameObject landType_LAND;
public GameObject hexagonCellPrefab;
public List<GameObject> hexagonCells = new List<GameObject>();
public List<GameObject> land = new List<GameObject>();
public int numberOfIslands;
public bool worldIsGenerated;
int i;
Vector3 hexagonPosition;
void Start()
{
GenerateMap();
}
void GenerateMap()
{
for (int x = 0,i = 0; x < mapSize; x++)
{
for (int z = 0; z < mapSize; z++)
{
HexagonCell hexCellInfo = landType_WATER.GetComponent<HexagonCell>();
CreateHexagonCell(x, z, i++,hexCellInfo);
worldIsGenerated = true;
}
}
if (worldIsGenerated)
{
CreateWater();
}
}
void CreateWater()
{
//Choose random number from a list delete the gameobject,empty space will be used to simulate water.Remaining objects will be used as land.
for (int o = 0; o < numberOfIslands; o++)
{
//Vygeneruj náhodné číslo mezi 0 a maximálním počtem hexagonů a vymaž je ze zásobníku
int randomNumber = Random.Range(o, (mapSize*mapSize));
var randomObject = new GameObject[numberOfIslands];
randomObject[o] = hexagonCells[randomNumber];
hexagonCells[randomNumber] = hexagonCells[o];
hexagonCells[o] = randomObject[o];
GameObject hexagonCell_Land = Instantiate<GameObject>(landType_LAND) as GameObject;
land.Add(hexagonCells[o]);
Destroy(hexagonCells[o]);
//hexagonCells.RemoveAll(GameObject => GameObject == null);
hexagonCells.RemoveAll(null);
}
}
void CreateHexagonCell(int x,int z,int i,HexagonCell hexCellInfo)
{
newHexagonPosition(x, z, i, hexCellInfo);
//Create cell and asign under map generator parent
GameObject hexCell = Instantiate<GameObject>(hexagonCellPrefab) as GameObject;
hexagonCells.Add(hexCell);
hexCell.transform.SetParent(transform, false);
hexCell.transform.localPosition = hexagonPosition;
}
void newHexagonPosition(int x,int z,int i,HexagonCell hexCellInfo)
{
hexagonPosition.x = (x + z * 0.5f - z / 2) * (hexCellInfo.innerHexagonRadius * 2f);
hexagonPosition.y = 0;
hexagonPosition.z = z * (hexCellInfo.outerHexagonRadius * 1.5f);
}
}