loop index error? JS

So I’m not entirely sure why this is happening, because it has been working for a few days and then I opened unity today and after cleaning up a few scripts that seemed rather unconnected, the function of crafting a weapon stopped working and returned this error:

ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.
Parameter name: index

I included a debug.log and it seems that the problem might be with my remove items after crafting function that loops through the inventory and checks if the required item is there, and if it is, it removes it. the inventory is simply an array of strings (not a bulletin array, but a normal mutable one). The debug.log is indicating that it is able to loop through and remove two of the three items, and then the game stops because this error message is returned. here’s the script:

function removeItems()
	removed = true;
	var psText : Text = powerSource.GetComponentInChildren.<Text>();
	initialBagLength = inventory.itemBag.length;
	for (var i = 0; i<initialBagLength; i++)
		if (inventory.itemBag *!= null)*
  •  {*
  •  	Debug.Log("item removed. length is now " + inventory.itemBag.length + "index is now " + i);*

_ if (inventory.itemBag == requiredStock)_
* {*
* inventory.itemBag.RemoveAt(i);*
* }*
_ if (inventory.itemBag == requiredBarrel)
* {
if (inventory.itemBag == psText.text)

pstext just refers to the text that is user inputted into a variable slot for the power core during the crafting process. I made an initial bag length variable at the beginning of the script and defined it here, thinking the shortening length might be the problem, but it didn’t seem to fix it. Unless it some other loop that consistently interferes with this loop when it gets to the point of detecting the third object for deletion. It doesn’t seem to matter whether or not I have more items in my inventory than just the three required and it always prints that the first two items are removed and then fails at the third, regardless of the order in which the items are removed (based on the order i picked them up in). Not sure what exactly is happening, but any help is appreciated._

Well let’s consider your case where you have 3 objects and are trying to remove all 3.

You start with {a, b, c} and i=0 so you remove the first object, a. Now you have {b, c} and i=1 so you remove the second object, c (uh oh). Now you have {b} and i=2 and are attempting to remove the third object from a list that only contains one item, which you can’t.

You have to take into account that the list changes as you add and remove items. For instance

for (var i = 0; i<inventory.itemBag.length;)
    if (inventory.itemBag *!= null&&*

(inventory.itemBag == requiredStock||inventory.itemBag == requiredBarrel||inventory.itemBag == psText.text)){
Debug.Log("item removed. length is now " + inventory.itemBag.length + "index is now " + i);
This code only increments i if the object is not deleted and compares i to the actual length of the list instead of the length at start. If an object is deleted from the list then the next object is already at i and incrementing i skips checking it. In addition, if you delete an object the the size of the list changes so checking the entire original list means you would be checking indicies that are no longer there.