- Home /
How to remove objects from a list ?
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);
}
}
Answer by JPhilipp · Feb 15, 2020 at 07:31 PM
Have you tried using the List functions Remove(list item)
or RemoveAt(item index)
?
I have tred using that, but ins$$anonymous$$d of removing the object from the list it will only destroy and remove only 1 object and the rest doesnˇt get destroyed or removed. I even tried reasigning objects into different list but the result is the same
You have to pass in the index or a reference to the object you want to remove from the list. It wont "destroy" unless there are no more references to it and if its a GameObject thats in your scene there will be a reference to it until its destroyed.
Answer by TjazS · Jan 02, 2021 at 01:30 PM
I was looking for an answer as well and came across this https://answers.unity.com/questions/589066/removing-from-a-list.html here is one of my scripts with a working deleting list. Also ik this post is old but probably in future mit might help some people. Sorry for the wierd code layout. Im still learning with this forums.
public List heliRockets = new List(); int rocket; int rocketsLeft; void Start() { rocketsLeft = heliRockets.Count; }
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse1) && rocketsLeft > 0)
{
rocketsLeft--;
rocket = Random.Range(0, rocketsLeft);
GameObject currentRocket = heliRockets[rocket];
heliRockets.Remove(currentRocket);
print(currentRocket);
}
}
Answer by ugotstoopt · Feb 21, 2021 at 11:31 PM
Use a coroutine / IENumerator to clear your list, instead of as part of the Update function.
For example, instead of where you have hexagonCells.RemoveAll(null);
, you could replace with StartCoroutine(RemoveCells());
Then have a separate IEnumerator that looks something like this:
public IEnumerator RemoveCells()
{
yield return 0;
hexagonCells.RemoveAll(item => item == null);
}
I hope this works for you. Something like this worked for me when I came across a similar problem.
The reason why is that the Destroy(GameObject) function doesn't truly destroy the object until the end of the current Update frame. So the 'yield return 0;' allows that to take place and then the list can be cleared.
You can see here in the documentation where Unity's documentation mentions "Actual object destruction is always delayed until after the current Update loop, but is always done before rendering": https://docs.unity3d.com/ScriptReference/Object.Destroy.html