Generic Lists, Item adding and removing options

I’ve recently had to dive headfirst into generic lists to work out the inventory system in my game. I am trying to make it so that when I remove an item from my inventory list it will leave an empty placeholder, and when the next item is added it will fill in the first blank that is available.

I have it set up to where each element of the list is showing up on a new line, and I want to make is that when I use up all of that item the spot where that item was is blank and leaves a space, and when a new item is added it takes up the first space available, if there are no spaces it would drop it on the end of the list.

Back when I was working on my enemy targeting scripting this functionality kind of worked automatically. I would have 4 or 5 GameObjects put into the list, and when I destroyed one of them their spot in the list would be blank and empty. The size of the list would remain the same, just with an empty slot where the now dead enemy was. Im not sure if this was a side effect of the destroy function, or if it was just an older version of unity, but when I remove an element from my list the size of list goes down as well.

I am currently using this script to remove the item from the list :

genericList.Remove(theItem);

Im not sure if theres a simpler way to script this to get the result I am looking for that I didn’t find in the documentation, but I am working on a workaround for it.

I have an item that works as null name nothing that I replace the item I want to remove, but I am not sure how to make it so that when I add a new item it will replace the null item.

I was thinking a for loop might work that would check for the name nothing, but I am not sure how to make it only add the new item to the first instance of it.

If there is a much simpler way to do it (like a different remove method) that would be great, but if not how would I make the the next added item replace the first null item?

Thanks in advance, sorry if I’m being clear enough.

EDIT:

Yeah, I had figured out a way to get the removed item replaced, though you code is much more logical than what I ended up with. I was looking through that link and it looks like .FInd should be right way to go. for replacing the empty space. I guess I need to know what a predicate function is and how to get it to work. I would guess its not as easy as

genericList.Find(theItem);

Use this page

to learn all the many handy functions you have available with List

the simplest, clearest approach. Use

IndexOf

to find the index of the item you are interested in.

So,

whereIsIt = Inventory.IndexOf( “Laser Cannon” );

then, whereIsIt is, say, 3

Then you can change the item.

Inventory[ whereIsIt ] = “Placeholder”;

it’s that simple.

So, before:

BBGun
Placeholder
Grenade Launcher
Laser Cannon
Beretta
Superwind
Placeholder
Placeholder
Microblaster
Soundwave
Flechette Device
Glue Gun

and afterwards

BBGun
Placeholder
Grenade Launcher
Placeholder
Beretta
Superwind
Placeholder
Placeholder
Microblaster
Soundwave
Flechette Device
Glue Gun

In my example, they are all strings. That’s perfectly reasonable. If you use enum, classes, structs, whatever, just the same.