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)
* {
inventory.itemBag.RemoveAt(i);
}
if (inventory.itemBag == psText.text)
{
inventory.itemBag.RemoveAt(i);
}
}
}
}*
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._