- Home /
Error when transferring items from one list to another
I have an inventory and chest system setup and it works pretty good for the most part. When I click an item in the chest it goes to my inventory, and vice versa. But if the chest has more items than my inventory, then I get an "argument is out of range" error and I can't figure out why.
Here's a piece of it
for(int a = 0;a < GameItems.Count;a++) {
if(GUILayout.Button(GameItems[a].name)) {
ChestItems.Add(GameItems[a]);
GameItems.Remove(GameItems[a]);
}
}
My Whole Scripts:
@gjf "ArgumentOutOfRangeException: Argument is out of range. Parameter name: index"
Answer by MarioAGOrdiano · Jul 07, 2014 at 10:30 PM
On the for loop after this one you write:
for(int a = 0;a < ChestItems.Count;a++) {
if(GUILayout.Button(GameItems[a].name)) {
GameItems.Add(ChestItems[a]);
ChestItems.Remove(ChestItems[a]);
}
so if ChestItems.Count us bigger than the count of GameItems when you check the name It'll return an index out of range exception.
I might be wrong but shouldn't the second loop check ChestItems for the name?
Your answer
![](https://koobas.hobune.stream/wayback/20220613153500im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Adding more than one item to the list and saving it into JSON file problem 1 Answer
How do i convert this code snippet to a list? 1 Answer
Access list storing custom class variables from another script 1 Answer
Keep list of GameObjects between scenes 2 Answers
C# - How to get only Distinct values (with threshold) and a specific count? 0 Answers