- Home /
generic list, swapping equipment errors but not always?
Im making and rpg style game with armors and weapons and such. Each "type" of equipment has a slot that it equips to. Each "slot" and the inventory is a Generic List. so in short,Im pushing items in and out of List each time i gain and item, equip and item, sell or remove an item from current equipment slots.
Its works and all, but i get an "Argument out of range exception." any idea why?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour {
private List<Consumables> bag;
private List<Armor> armour;
private List<Item> items;
private List<Item> tools;
private List<Armor> armourSlot;
//public List<Weapon> weaponSlot;
private List<Armor> helmetSlot;
public Player playerScript;
public GUISkin customSkin;
private Rect windowRect = new Rect(0, 0, 200, 350);
private Rect windowRect1 = new Rect(0, 0, 200, 214);
public bool invenOpen= false;
public bool characterMenu = false;
private int buttonSize = 35;
private int activeSlot;
public int gold = 0;
public Vector2 scrollPosition;
private string page = "Foods";
public int defValArmor = 0;
public int defValHelmet = 0;
//bag//
public Consumables beef;
public Consumables cabbage;
//Item
public Armor bronzeArmor;
public Armor bronzeHelmet;
void Start(){
if (bag == null){
bag = new List<Consumables>();
}
if (armour == null){
armour = new List<Armor>();
}
if (armourSlot == null){
armourSlot = new List<Armor>();
}
if (helmetSlot == null){
helmetSlot = new List<Armor>();
}
if (items == null){
items = new List<Item>();
}
if (tools == null){
tools = new List<Item>();
}
if(networkView.isMine){
invenOpen = false;
}
bag.Add (beef);
armour.Add (bronzeArmor);
armour.Add (bronzeHelmet);
}
void Update(){
if(Input.GetKeyUp (KeyCode.I)&& networkView.isMine){
invenOpen = !invenOpen;
}
if(Input.GetKeyUp (KeyCode.C)&& networkView.isMine){
characterMenu = !characterMenu;
}
}
Rect ClampToScreen(Rect r)
{
r.x = Mathf.Clamp(r.x,0,Screen.width-r.width);
r.y = Mathf.Clamp(r.y,0,Screen.height-r.height);
return r;
}
void OnGUI(){
playerScript.defense = defValArmor + defValHelmet;
GUI.skin = customSkin;
if(invenOpen){
windowRect = ClampToScreen(GUI.Window(0,windowRect,DoInventoryWindow,"Inventory"));
}
if(characterMenu){
playerScript.defense = defValArmor + defValHelmet;
windowRect1 = ClampToScreen(GUI.Window(2, windowRect1, DoInventoryWindow3, "Character"));
}
}
void useFood(int mySlot){
int heal = bag [mySlot].healAmount;
playerScript.TakeHeal(heal);
bag.RemoveAt(mySlot);
return;
}
void useArmor(int mySlot, int eqSlot){
if(eqSlot == 1){
if(armourSlot.Count == 0){
armourSlot.Add(armour[mySlot]);
armour.RemoveAt(mySlot);
defValArmor = armourSlot [mySlot].defBonus;
return;
}
if(armourSlot.Count == 1){
armour.Add(armourSlot[0]);
armourSlot.RemoveAt(0);
defValArmor = armourSlot [mySlot].defBonus;
return;
}
}
if(eqSlot == 0){
if(helmetSlot.Count == 0){
helmetSlot.Add(armour[mySlot]);
armour.RemoveAt(mySlot);
defValHelmet = helmetSlot [mySlot].defBonus;
return;
}
if(helmetSlot.Count == 1){
armour.Add(helmetSlot[0]);
helmetSlot.RemoveAt(0);
defValHelmet = helmetSlot [mySlot].defBonus;
return;
}
}
}
void useItem(int mySlot){
}
void useTool(int mySlot){
}
//INVENTORY WINDOW
void DoInventoryWindow(int windowID){
GUI.Box (new Rect (2, 328, 124, 20), "Gold: " + gold);
switch(page)
{
case "Foods":
GUI.Box (new Rect (121, 25, 77, 36), "");
if(GUI.Button (new Rect (121, 61, 77, 36), "Items")){
page = "Items";
}
if(GUI.Button (new Rect (121, 97, 77, 36), "Gear")){
page = "Gear";
}
if(GUI.Button (new Rect (121, 133, 77, 36), "Tools")){
page = "Tools";
}
break;
case "Items":
if(GUI.Button (new Rect (121, 25, 77, 36), "Foods")){
page = "Foods";
}
GUI.Box (new Rect (121, 61, 77, 36), "");
if(GUI.Button (new Rect (121, 97, 77, 36), "Gear")){
page = "Gear";
}
if(GUI.Button (new Rect (121, 133, 77, 36), "Tools")){
page = "Tools";
}
break;
case "Gear":
if(GUI.Button (new Rect (121, 25, 77, 36), "Foods")){
page = "Foods";
}
if(GUI.Button (new Rect (121, 61, 77, 36), "Items")){
page = "Items";
}
GUI.Box (new Rect (121, 97, 77, 36), "");
if(GUI.Button (new Rect (121, 133, 77, 36), "Tools")){
page = "Tools";
}
break;
case "Tools":
if(GUI.Button (new Rect (121, 25, 77, 36), "Foods")){
page = "Foods";
}
if(GUI.Button (new Rect (121, 61, 77, 36), "Items")){
page = "Items";
}
if(GUI.Button (new Rect (121, 97, 77, 36), "Gear")){
page = "Gear";
}
GUI.Box (new Rect (121, 133, 77, 36), "");
break;
}
if(page == "Foods"){
scrollPosition = GUI.BeginScrollView(new Rect(2, 23, 120, 302), scrollPosition, new Rect(0, 10, 53, bag.Count * buttonSize));
if(bag.Count >= 1){
for(int i = 0; i < bag.Count; i++){
if (GUI.Button (new Rect (0, 12 + i * buttonSize, buttonSize * 3, buttonSize), bag [i].icon)) {
activeSlot = i;
useFood(activeSlot);
}
}
}
GUI.EndScrollView();
}
if(page == "Gear"){
scrollPosition = GUI.BeginScrollView(new Rect(2, 23, 120, 302), scrollPosition, new Rect(0, 10, 53, armour.Count * buttonSize));
if(armour.Count >= 1){
for(int i = 0; i < armour.Count; i++){
if (GUI.Button (new Rect (0, 12 + i * buttonSize, buttonSize * 3, buttonSize), armour [i].icon)) {
activeSlot = i;
useArmor(activeSlot, armour[i].equipSlot);
}
}
}
GUI.EndScrollView();
}
if(page == "Items"){
scrollPosition = GUI.BeginScrollView(new Rect(2, 23, 120, 302), scrollPosition, new Rect(0, 10, 53, items.Count * buttonSize));
if(items.Count >= 1){
for(int i = 0; i < items.Count; i++){
if (GUI.Button (new Rect (0, 12 + i * buttonSize, buttonSize * 3, buttonSize), items [i].icon)) {
activeSlot = i;
useItem(activeSlot);
}
}
}
GUI.EndScrollView();
}
if(page == "Tools"){
scrollPosition = GUI.BeginScrollView(new Rect(2, 23, 120, 302), scrollPosition, new Rect(0, 10, 53, tools.Count * buttonSize));
if(tools.Count >= 1){
for(int i = 0; i < items.Count; i++){
if (GUI.Button (new Rect (0, 12 + i * buttonSize, buttonSize * 3, buttonSize), tools [i].icon)) {
activeSlot = i;
useTool(activeSlot);
}
}
}
GUI.EndScrollView();
}
GUI.DragWindow(new Rect(0, 0, 10000, 10000));
}
void DoInventoryWindow3(int windowID){
GUI.Box(new Rect(80,50,100,20), "Defense "+ playerScript.defense);
if(armourSlot.Count >= 1){
if (GUI.Button (new Rect (2, 50, buttonSize, buttonSize), armourSlot [0].icon)) {
if(armour.Count <=99){
armour.Add (armourSlot[0]);
armourSlot.RemoveAt(0);
defValArmor = 0;
}
}
}
if(helmetSlot.Count >= 1){
if (GUI.Button (new Rect (2, 14, buttonSize, buttonSize), helmetSlot [0].icon)) {
if(armour.Count <=99){
armour.Add (helmetSlot[0]);
helmetSlot.RemoveAt(0);
defValHelmet = 0;
}
}
}
GUI.DragWindow(new Rect(0, 0, 10000, 10000));
}
}
this is the error i got last time i tried:
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[Armor].get_Item (Int32 index) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
Inventory.useArmor (Int32 mySlot, Int32 eqSlot) (at Assets/Scripts/Inventory.cs:100)
Inventory.DoInventoryWindow (Int32 windowID) (at Assets/Scripts/Inventory.cs:210)
UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/GUI.cs:1395)
I code-formatted your debug errors. The actual error is inside the List code itself. There the exception is generated (Line 633)
The next step in the stacktrace is inside useArmor on line 100 where you index the armourSlot list. That this is where the error comes from.
Of course useArmor is executed from DoInventoryWindow in line 210 and DoInventoryWindow is executed from the Unity window system, but that's quite irrelevant here. This is sometimes useful if something gets called which shouldn't get called and you don't know who or from where it's called.
Answer by Bunny83 · Mar 26, 2014 at 10:56 AM
You read the debug information wrong ;) The error is in line 100. Line 210 is just the point which executes the "useArmor" function.
defValArmor = armourSlot [mySlot].defBonus;
This is the offending line and the problem is that you use mySlot as index into the armourSlot list. I guess you just want to access the "thing" you just added to the list, so use:
armourSlot[armourSlot.Count-1]
instead.
armourSlot will never be bigger than one in length so i just chose "0" ins$$anonymous$$d of armourSlot.Count-1. never would have guessed it would be so simple. :P Thanks a ton.
Just want to add that you have another logical problem in line 106 as well ;) If armourSlot has 1 object and you're removing that object you can't access any objects in the armourSlot list since it's empty. The same problem with the helmetSlot ;)
Thank you. I fixed that too. :P $$anonymous$$y game is working perfectly.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141215im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Add random amount of random items from one list to another? 2 Answers
How to ignore base class? 1 Answer
check if list contains item with matching string property 2 Answers
Lists as Parameters? 1 Answer