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 Lakeyour · Jan 23, 2016 at 12:39 PM · c#inventoryinventory system

Argument out of range?

Hey there, I've been trying to solve an Inventory System these past weeks.

(Most of this is from a tutorial, which I have expanded alot)

However there's a few bugs which causes an Argument is out of range to trigger.

This is specifically when I press the button which opens the Inventory.

Unity is not doing a great job in telling me what is causing this either.

I should add that I have a separate script for picking up items, but it does not work yet.

Here's alot of code (Gibberish if you will):


Specific lines it mentions in one such error.

 Item item = inventory [i];
 DrawInventory ();

Inventory.cs

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 using Sfs2X.Entities;
 using Sfs2X.Entities.Variables;
 using Sfs2X.Entities.Data;
 using Sfs2X.Requests;
 
 public class Inventory : MonoBehaviour
 {
     public int slotsX, slotsY;
     public GUISkin skin;
     public static List<Item> inventory = new List<Item>(); // List
     public List<Item> slots = new List<Item>(); // List
     private bool showInventory; // Show Inventory Bool
     public ItemDatabase database; // Item Database
     private bool showTooltip; // Tooltip Bool
     private string tooltip; // Tooltip String
     private bool dropItem; // Drop Item Bool
     private bool spawnItem;
     //private bool takeItem;
 
     private bool draggingItem;
     private Item draggedItem;
     private int prevIndex; // Previous Index
 
 
     // Use this for initialization
     public void Start()
     {
         database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
         for (int i = 0; i < (slotsX * slotsY); i++)
         {
             inventory.Add(new Item());
         }
     }
 
     void Update() // Show the inventory if we press the key I
     {
         if (Input.GetButtonDown("Inventory"))
         {
             showInventory = !showInventory;
 
             ItemDatabase.FetchItemBySlug("potion");
         }
     }
 
     void OnGUI() // Tooltip, Draw Inventory and Item Drag Icon
     {
         tooltip = "";
         GUI.skin = skin;
         if (showInventory)
         {
             DrawInventory ();
             if (showTooltip)
             {
                 GUI.Box(new Rect(Event.current.mousePosition.x + 15, Event.current.mousePosition.y + 10, 200, 200), tooltip, skin.GetStyle("Tooltip"));
                 showTooltip = false;
             }
         }
         if (draggingItem)
         {
             GUI.DrawTexture(new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 50, 50), draggedItem.itemIcon);
         }
         if (dropItem) {
             
                 DropItem (this.draggedItem);
             }
         }
 
 
     void DrawInventory() // Draw the inventory, allow dragging and dropping of items.
     {
         Event e = Event.current;
         int i = 0;
         for (int y = 0; y < slotsY; y++) {
             for (int x = 0; x < slotsX; x++) {
                 Rect slotRect = new Rect (x * (45 + 12), y * (45 + 12), 57, 57);
                 GUI.Box (slotRect, "", skin.GetStyle ("Inventory"));
                 Item item = inventory [i];
                 if (item.itemName != null) 
                 {
                     if (item.itemIcon != null) GUI.DrawTexture (slotRect, inventory [i].itemIcon);
                     if (item.itemquantity > 1) {
                         GUI.Label (slotRect, inventory [i].itemquantity.ToString ());
                     }
                     if (slotRect.Contains (e.mousePosition)) {
                         tooltip = CreateTooltip (inventory [i]);
                         showTooltip = true;
                         if (e.button == 0 && e.type == EventType.mouseDrag && !draggingItem) {
                             draggingItem = true;
                             prevIndex = i;
                             draggedItem = inventory [i];
                             inventory [i] = new Item ();
                         }
                         if (e.type == EventType.mouseUp && draggingItem) {
                             inventory [prevIndex] = inventory [i];
                             inventory [i] = draggedItem;
                             draggingItem = false;
                             draggedItem = null;
                         }
                         if (e.isMouse && e.type == EventType.mouseDown && e.button == 1)
                         if (item.itemType == Item.ItemType.Consumable) {
                             UseConsumable (inventory [i], i);
                         }
                     }
                     i++;
                 } else {
                     if (slotRect.Contains (e.mousePosition)) {
                         if (e.type == EventType.mouseUp && draggingItem) {
                             inventory [i] = draggedItem;
                             draggingItem = false;
                             draggedItem = null;
                             
                         }
                     } else if (e.type == EventType.mouseUp && draggingItem && spawnItem == false && (e.mousePosition.x > slotsX * (45 + 12) || e.mousePosition.y > slotsX * (45 + 12))) {
                         inventory [prevIndex] = new Item ();
                         dropItem = true;
                         spawnItem = true;
                         break;
                     }
                 }
                             if (tooltip == "") {
                                 showTooltip = false;
                             }
                             i++;
                         }
                     }
                 }
             
         
     string CreateTooltip(Item item)
     {
         tooltip = "";
         if (item.itemType == Item.ItemType.Weapon)
         {
             tooltip += "<color=#FFFFFF>" + item.itemName + "</color>\n\n" + "<color=#FFFFFF>" + item.itemDamage + " Damage" + "</color>\n\n" + item.itemDesc + "\n" + item.itemType + "\n"; // Draw Tooltip, Text Color and Attributes
         }
         if (item.itemType == Item.ItemType.Consumable)
         {
             tooltip += "<color=#FFFFFF>" + item.itemName + "</color>\n\n" + "<color=#FFFFFF>" + "</color>\n\n" + item.itemDesc + "\n" + item.itemType + "\n"; // Draw Tooltip, Text Color and Attributes
         }
         if (item.itemType == Item.ItemType.Quest)
         {
             tooltip += "<color=#FFFFFF>" + item.itemName + "</color>\n\n" + "<color=#FFFFFF>" + "</color>\n\n" + item.itemDesc + "\n" + item.itemType + "\n"; // Draw Tooltip, Text Color and Attributes
         }
         return tooltip;
     }
 
 
 
     public void RemoveItem(int id) // Remove Item
     {
         for (int i = 0; i < inventory.Count; i++)
         {
             if (inventory[i].itemID == id)
             {
                 inventory[i] = new Item();
                 break;
             }
         }
     }
 
     void AddItem(int id) // Add Item
     {
         for (int i = 0; i < inventory.Count; i++)
         {
             if (inventory[i].itemName == null)
             {
                 for (int j = 0; j < database.items.Count; j++)
                 {
                     if (database.items[j].itemID == id)
                     {
                         inventory[i] = database.items[j];
                     }
                 }
                 break;
             }
         }
     }
 
     void DropItem(Item item) //Drop Item
     {
         if (spawnItem == true) {
             Debug.Log (item.itemName + " dropped");
 
             //Here goes the send mmoitem to server code..
             //The position to spawn;  newVector2(NetworkLogin.GetLocalPlayer().transform.position.x,            NetworkLogin.GetLocalPlayer().transform.position.y)
 
 
             ISFSObject objt = SFSObject.NewInstance ();
 
             objt.PutUtfString ("name", item.itemName);
             objt.PutInt ("id", item.itemID);
             objt.PutUtfString ("desc", item.itemDesc);
             objt.PutInt ("damage", item.itemDamage);
             objt.PutInt ("speed", item.itemSpeed);
             objt.PutInt ("health", item.itemRestore);
             objt.PutUtfString ("type", item.itemType.ToString ());
             objt.PutBool ("stackable", item.itemstackable);
             objt.PutInt ("rarity", item.itemrarity);
             objt.PutUtfString ("slug", item.itemslug);
 
             objt.PutFloat ("x", NetworkLogin.GetLocalPlayer ().transform.position.x);
             objt.PutFloat ("y", NetworkLogin.GetLocalPlayer ().transform.position.y);
 
             SFS2X_Connect.sfs.Send (new ExtensionRequest ("SpawnMMOItem", objt));
 
             spawnItem = false;
         }
         draggingItem = false;
         dropItem = false;
         draggedItem = null;
         
     }
 
 
     public bool InventoryContains(int id)
     {
         foreach (Item item in inventory)
             if (item.itemID == id) return true;
         return false;
 
     }
 
 
     private void UseConsumable(Item item, int slot)
     {
         if(item.itemquantity > 1)
         {
             item.itemquantity--;
             Debug.Log("You used " + item.itemName + " and gained " + item.itemRestore + " health.");
         }
 
         else
         {
             inventory[slot] = new Item();
             Debug.Log("You used " + item.itemName + " and gained " + item.itemRestore + " health.");
         }
 
     }
 
 }






Comment
Add comment · Show 2
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 ShadyProductions · Jan 23, 2016 at 01:03 PM 0
Share

The list's index you are trying to access doesn't exist, thats why you get an argument out of range. Have you set your slotsx & slotsy in the inspector?

avatar image Lakeyour ShadyProductions · Jan 23, 2016 at 01:18 PM 0
Share

I'll take a look and reply again to you, thanks for the suggestion!

EDIT: The slots has been set to 4x4! :)

2 Replies

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

Answer by alishka · Jan 23, 2016 at 01:25 PM

Hey!

Set i to -1 instead of 0 at line 76, then try incrementing your i on line 108 right after the second for loop on line 78. Should do the trick.

And yes as you have mentioned above, the code is messy. Consider refactoring or even rewriting from scratch.

Cheers!

Comment
Add comment · Show 10 · 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 Lakeyour · Jan 23, 2016 at 01:31 PM 0
Share

I'll try what you said and reply if it works!

And yeah the code is a giant mess of gibberish

avatar image Lakeyour Lakeyour · Jan 23, 2016 at 01:32 PM 0
Share

Unfortunately did not resolve the issue, thanks though.

avatar image ShadyProductions · Jan 23, 2016 at 01:32 PM 0
Share

doing that it will never access index 0. for some reason the value is just not set in index 0 let alone the other indexes

avatar image ShadyProductions ShadyProductions · Jan 23, 2016 at 01:39 PM 0
Share

try checking after line 80

 if (item != null) print("exists!") else return

This way you'll know if the index exists or not.

avatar image Lakeyour ShadyProductions · Jan 23, 2016 at 01:41 PM 0
Share

Aye, I'll give it a try, back in a $$anonymous$$ute!

Show more comments
avatar image alishka ShadyProductions · Jan 23, 2016 at 02:14 PM 0
Share

Forgot to add: "set i = -1 ins$$anonymous$$d of i = 0 :)

avatar image Lakeyour alishka · Jan 23, 2016 at 02:15 PM 0
Share

Ah okay, I'll try it out

Show more comments
avatar image
0

Answer by Lakeyour · Jan 23, 2016 at 08:45 PM

Solution for the out of range Argument

Changed

 for (int i = 0; i < (slotsX * slotsY); i++)

To

 for (int i = -1; i < (slotsX * slotsY); i++)
 

It does NOT return -1 ever.

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 Lakeyour · Jan 23, 2016 at 02:25 PM 0
Share

Thanks for the help folks!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Inventory Script 0 Answers

How to make an Inventory Hotbar (C#) 0 Answers

Help with Interface-free inventory 0 Answers

Why is drag and drop not working? 1 Answer

Canvas Inventory closing 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