Destroy a gameobject referenced in list and remove that element

Hi guys,
In order to pick up an item (one among multiple ones) on the floor, I dynamically create a list of item in player range (i get a reference to each item).

I’d like to be able to destroy the closest (in terms of distance from player).
How would you do that ? And if I get the right item wouldn’t it be tricky to just destroy the object without removing it from list ?

thanks !!

If you invoke GameObject.Destroy(myItems[myIndex]) the spot in the list will be occupied, but there “cell” will still contain null. You will then have to remove this empty entry by calling myItems.RemoveAt(myIndex)

Also, if I am not wrong, unity has an issue where even if you call Destroy, the garbage collector won’t free the memory. For that to happen, after Destroy(), you have to manually assign null to the variable which used to point to the destroyed gameObject.

Also, you might want to sort the list every few frames, by distance to player. You should do this when you have spare FPS. That way you don’t have to do distance comparison for every element in the list, if you need to do this many times during the frame. You just sort once, and then remove 1st element.

Use QuickSort if the list is almost sorted, Merge sort if it’s badly sorted

Scribbled this up in note pad, so not checked the syntax but it should be correct. Assuming your list of a MonoBehaviour type (in this example a fictional class called “Item”) then you can do this.

public void DestroyNearest(List<Item> items)
	// Find nearest item.
	Item nearest = null;
	float distance = 0;
	for(int i=0;i<items.Count;i++)
		float tempDistance = Vector3.Distance(transform.position, items*.position);*
  •  if(nearest == null || tempDistance < distance)*
  •  {*

_ nearest = items*;_
distance = tempDistance;_

* // Remove from list.*
* items.Remove(nearest);*

* // Destroy object.*
* Destroy(nearest.gameObject);*
The basic premise is iterate all the items to locate the nearest one, storing a reference to it, then remove the reference from the list, and finally use the stored reference to then destroy the item. You can of course comment out/remove the line that removes the item from the list, but be warned that assuming the list of items are references to game objects then you will end up with a “null” item inside that list, which you would need to account for in any code (like this one) that iterates over the collection.