Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Punkjim420 · Mar 26, 2014 at 09:49 AM · c#listinventoryout of range

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)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Mar 26, 2014 at 11:02 AM 1
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Punkjim420 · Mar 26, 2014 at 11:08 AM 0
Share

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.

avatar image Bunny83 · Mar 26, 2014 at 11:47 AM 2
Share

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 ;)

avatar image Punkjim420 · Mar 26, 2014 at 11:53 AM 0
Share

Thank you. I fixed that too. :P $$anonymous$$y game is working perfectly.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Method is called, but GUI doesn't show up 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges