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.");
}
}
}
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?
I'll take a look and reply again to you, thanks for the suggestion!
EDIT: The slots has been set to 4x4! :)
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!
I'll try what you said and reply if it works!
And yeah the code is a giant mess of gibberish
Unfortunately did not resolve the issue, thanks though.
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
try checking after line 80
if (item != null) print("exists!") else return
This way you'll know if the index exists or not.
Aye, I'll give it a try, back in a $$anonymous$$ute!
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.
Your answer
Follow this Question
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