Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by PvTGreg · Jul 07, 2016 at 08:58 PM · c#argumentoutofrangeexception

Inventory -Argument is out of range - Diagnostics to be done

Hi im trying to create an inventory and when adding a new slot and an item i get the Argument is out of range error. also when i try and add 2 items at the same time only the first one goes through.

 void Update(){
         if (Input.GetKeyDown (KeyCode.Space)) {
             if (inTrigger = true) {
                 if (TriggerTag.Equals ("AppleTree")) {
                     Collect ("Apple", "An Apple", 1, null, Item.ItemType.Consumeable);
                 }
             } else {
                 Debug.Log ("There is nothing to collect!");
             }
         }
     }

  void Collect(string itemName,string itemDescription,int itemAmount,   Sprite itemIcon, Item.ItemType type){
             this.GetComponent<Inventory> ().AddItem (itemName,itemDescription,itemAmount,itemIcon,type);
         }

The above passes in some information you need for an item to be added and now to the next script

 public void AddItem (string itemName,string itemDescription,int itemAmount,   Sprite itemIcon, Item.ItemType type){
         if (inventory.Count <= InventoryMaxSize) {
             if (inventory.Count == 0) {
                 inventory.Add (new Item (itemName, itemDescription, itemAmount, itemIcon, type));
                 AddSlot ();
                 UpdateGui();
             } else {
                 for (int i = 0; i < inventory.Count; i++) {
                     if (inventory [i].ItemName == itemName) {
                         inventory [i].ItemAmount = inventory [i].ItemAmount + itemAmount;
                         UpdateGui();
                         break;
                     } else {
                         AddSlot();
                         inventory.Add (new Item (itemName, itemDescription, itemAmount, itemIcon, type));
                         UpdateGui ();
                         break;
                     }
                 }
             }
         } 
     }
 
     public void AddSlot(){
             GameObject slot = (GameObject)Instantiate (SlotGO);
             Slots.Add (slot);
             slot.transform.parent = GameObject.FindGameObjectWithTag ("InvGUI").transform;
             slot.GetComponent<RectTransform> ().localPosition = new Vector3 (SlotStartPosX, SlotStartPosY, 0);
             slot.GetComponent<RectTransform> ().localScale = new Vector3 (1, 1, 1);
             SlotStartPosY = SlotStartPosY - SlotMoveDown;
     }
     public void UpdateGui(){
         for (int i = 0; i < Slots.Count; i++) {
             for (int x = 0; i < inventory.Count; x++) {
                 Slots [i].GetComponent<SlotScript> ().item.ItemName = inventory [x].ItemName;
                 Slots [i].GetComponent<SlotScript> ().item.ItemDescription = inventory [x].ItemDescription;
                 Slots [i].GetComponent<SlotScript> ().item.ItemAmount = inventory [x].ItemAmount;
                 Slots [i].GetComponent<SlotScript> ().item.ItemIcon = inventory [x].ItemIcon;
                 Slots [i].GetComponent<SlotScript> ().item.itemType = inventory [x].itemType;
             }
         }
     }


The error points to the UpdateGUI function. also if i call the collect function twice from the first script with different item names it only seams to add the first one. any help would be greatly appreciated

Comment
Add comment
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

1 Reply

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

Answer by btmedia14 · Jul 07, 2016 at 11:30 PM

In UpdateGui() method, control variable in for loop is increased but condition does not check it. Hence x continues to increase and eventually will lead to array index out of bounds error:

  for (int x = 0; i < inventory.Count; x++) 

try replacing with:

  for (int x = 0; x < inventory.Count; x++) 

Also, unclear why a second inner loop is needed since same Slots[i] is being replaced multiple times.

Comment
Add comment · Show 1 · 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 PvTGreg · Jul 07, 2016 at 11:55 PM 0
Share

thank you very much. silly mistake on my behalf haha:P guess looking at for ages it just slipped through.

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

166 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 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 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 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 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 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 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 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

ArgumentOutOfRangeException: Argument is out of range. -> Index 2 Answers

Unknown Argument Out of Range Index Error On Card Game 1 Answer

How do I get my Inventory Loading to work again? ArgumentOutOfRangeException 2 Answers

UNET ArgumentOutOfRangeException 0 Answers

Problem when acessing a list from another script? (ArgumentOutOfRangeException) 0 Answers


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