• 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
Question by thenachotech1113 · Dec 07, 2014 at 05:36 PM · guiinventory

grid style inventory giving me "Index out of range" problems

hello, its the first time i ever try to make an inventory and im not sure if this is the best way to make a grid stile inventory, here is the code i used

this script its the one in charge of opening the inventory and as an item database

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Item_DataBase : MonoBehaviour {
 
     public List <Item> items = new List<Item> ();
     public List <int> invItemsID = new List<int> ();
     public List <GameObject> slots = new List<GameObject>();
 
     public GameObject panel;
     public Material panelMat;
     public Color panelOnCol;
     public float currColor;
     public bool panelOn;
 
     public int maxXSprites;
     public int maxYSprites;
     public Vector2 buttonOffset;
 
     public float spriteCount;
 
     public GameObject slot;
     float refCol;
 
     void Start () {
         items.Add(new Item("Iridium", 0, 10, "An element"));
         items.Add(new Item("Uranium", 1, 20, "An other element"));
         items.Add(new Item("Coffe", 2, 30, "Cup o Joe"));
     }
     
     void Update () {
         if (Input.GetKeyDown(KeyCode.I) && panelOn == false) {
             panelOn = true;
             int x = 0;
             int y = 0;
 
             int currMaxX = 5;
             int currMaxY = invItemsID.Count / 5;
 
             int itemsLeft = invItemsID.Count;
 
             while (y < currMaxY){
                 while (x < currMaxX){
 
                     if (itemsLeft < 6)currMaxX = x%5; 
 
                     GameObject clone = Instantiate (slot) as GameObject;
                     clone.transform.SetParent(panel.transform, false);
                     Item_Script iS = clone.GetComponent<Item_Script>();
                     iS.myItemIndex = invItemsID[maxXSprites * (x - 1) + y];
                     float xPos = -127 + 55 * x;
                     float yPos = 90 + buttonOffset.y * y;
                     RectTransform rT = clone.GetComponent<RectTransform>();
                     rT.localPosition = new Vector3 (xPos, yPos, 0);
                     slots.Add(clone);
                     itemsLeft --;
                     x++;
 
                 }
                 y++;
             }
         }
         else panelOn = false;
 
 
     
     }
 
 }

and this is the item class

 using UnityEngine;
 using System.Collections;
 
 public class Item_Data : MonoBehaviour {
 
 
 }
 public class Item{
     
     public string itemName;
     public string itemDescription;
     public int itemID;
     public float itemPrice;
     
     
     public Item (string name, int id, float price, string description){
         
         itemName = name;
         itemDescription = description;
         itemID = id;
         itemPrice = price;
         
     }
 }
 
 

the line 51 of the first script, where it says "iS.myItemIndex = invItemsID[maxXSprites * (x - 1) + y];" is giving me an index out of range error in the editor. i have revised this script several times and cant find what is wrong. Sorry for the long question, and thanks a lot for your time.

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by RudyTheDev · Dec 07, 2014 at 06:36 PM

Without reading all the code, I would expect the line to be something like invItemsID[maxXSprites * y + x]; if x and y start at 0.

Assuming, you count x left to right, each row has x cells. You count y top to bottom. So every row is x cells. maxXSprites is (presumably) number of cells in a row, in other words, column count. So maxXSprites * y is how many total cells are in y rows. Then adding x for the current/last row.

Of course, the rest of the code needs the same logic applied. x is counting columns and y is counting rows.

Also see this answer implementing a grid inventory that I have previously answered.

Comment
thenachotech1113

People who like this

1 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 thenachotech1113 · Dec 09, 2014 at 07:17 PM 0
Share

thanks a lot

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

26 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

Related Questions

How to remove white border in Canvas? 1 Answer

need to shorten my code but unsure of how 3 Answers

proper inventory system issue.. 1 Answer

how to change a gui button texture 1 Answer

How To Make A Numeric Inventory System? 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