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

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 inventory = new List(); // List
public List slots = new List(); // 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();
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 ;_
* if (item.itemName != null)*
* {*
_ if (item.itemIcon != null) GUI.DrawTexture (slotRect, inventory .itemIcon);
* if (item.itemquantity > 1) {
GUI.Label (slotRect, inventory .itemquantity.ToString ());
}
if (slotRect.Contains (e.mousePosition)) {
tooltip = CreateTooltip (inventory );
showTooltip = true;
if (e.button == 0 && e.type == EventType.mouseDrag && !draggingItem) {
draggingItem = true;
prevIndex = i;
draggedItem = inventory ;
inventory = new Item ();
}
if (e.type == EventType.mouseUp && draggingItem) {
inventory [prevIndex] = inventory ;
inventory = 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++;
} else {
if (slotRect.Contains (e.mousePosition)) {
if (e.type == EventType.mouseUp && draggingItem) {
inventory = 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=#FFFFFF>” + item.itemDamage + " Damage" + "

" + item.itemDesc + "
" + item.itemType + "
"; // Draw Tooltip, Text Color and Attributes*

}
if (item.itemType == Item.ItemType.Consumable)
{
*tooltip += “<color=#FFFFFF>” + item.itemName + "

" + “<color=#FFFFFF>” + "

" + item.itemDesc + "
" + item.itemType + "
"; // Draw Tooltip, Text Color and Attributes*

}
if (item.itemType == Item.ItemType.Quest)
{
*tooltip += “<color=#FFFFFF>” + item.itemName + "

" + “<color=#FFFFFF>” + "

" + item.itemDesc + "
" + item.itemType + "
"; // 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*.itemID == id)*
{
inventory = new Item();
break;
}
}
}

void AddItem(int id) // Add Item
{
for (int i = 0; i < inventory.Count; i++)
{
if (inventory*.itemName == null)*
{
for (int j = 0; j < database.items.Count; j++)
{
if (database.items[j].itemID == id)
{
inventory = 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.”);
}

}

}

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!

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.