Skip to main content
2 of 2
Focusing on the question, which is not about clearing - you already know how to do that.
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Removing all destroyed objects from a list

I am a little confused on deleting objects in a List. I realise that clearing the list doesn't destroy the gameobject in Unity. Hence I have written this routine to destroy and remove:

//clear all block sprites away for (int i = 0; i < blockSprites.Count; i++) { Destroy(blockSprites[i]); } blockSprites.Clear(); 

But what if I only wanted to destroy some of the elements (a variable amount)?

In that scenario the List.Clear() function wouldn't work. Would I have to make another list of indexes in order to go through destroying and removing from the list in two sweeps. Or is there a better way?

I_Keep_Trying
  • 355
  • 1
  • 4
  • 23