need some help with ArgumentOutOfRangeException

Hi all, my script here is supposed to act as a store where players can buy and sell items.


import System.Collections.Generic;

var x : int;
var cnt : int;

var Player;
var InventoryArray;
var Inventory;
var gold;

var showGUI = false;
var showarmorGUI = false;
var showweaponsGUI = false;
var showmiscGUI = false;

var storeInventory = new List.<Item>();
var Items : Items;

var playerInventory;

//private var playerInventory : InventoryList; // reference to the inventory

function Start() {

playerInventory = GameObject.Find("GUIElements").GetComponent("Inventory").inventory;
for(cnt = 0; cnt < x; cnt++) { 
storeInventory.Add(Items.StoreInventory1[cnt]); <<<ERROR OCCURS HERE

function OnGUI() {
if(showGUI == true) {

GUI.Box(Rect(60,60,265,30),"Welcome, stranger. What can I get for you?");

showarmorGUI = true;

showweaponsGUI = true;

showmiscGUI = true;


enum Rarity{Common, Uncommon, Rare, Beastly, Insane, Epic, Legendary, Null}
enum ArmorSlots{Head, Neck, Shoulders, Arms, Forearms, Hands, Torso, Waist, Legs, Feet, Null}
enum JewelrySlots{Ear1, Ear2, Neck, Wrist, Finger1, Finger2, Finger3, Finger4, Finger5, Finger6, Finger7, Finger8, Finger9, Finger10, Null}
enum WeaponSlot{Primary, Secondary, Ranged, Null}
enum WeaponSlot2{Primary, Secondary, Ranged, Null}

//var name1 : String;

//var _value : int;
var name2 : String;
var description : String;
var rarity : Rarity;
var price : int;
var armorSlot : ArmorSlots;
var jewelSlot : JewelrySlots;
var weaponSlot : WeaponSlot;
var weaponSlot2 : WeaponSlot2;
var curDura : int;
var maxDura : int;
var icon : Texture2D;

function Start() {

class Item {

function Item(_name2,_description,_rarity,_armorSlot,_weaponSlot,_weaponSlot2,_jewelSlot,_curDura,_maxDura,_icon,_price) {

	item_name2 = _name2;
	item_description = _description;
	item_rarity = _rarity;
	item_armorSlot = _armorSlot;
	item_weaponSlot = _weaponSlot;
	item_weaponSlot2 = _weaponSlot2;
    item_jewelSlot = _jewelSlot;
    item_curDura = _curDura;
    item_maxDura = _maxDura;
    item_icon = _icon;
    item_price = _price;
	//item_rarity = Rarity.Rare;
	//item_maxdura = maxDura;
	//item_curdura = curDura;




import System.Collections.Generic;

var sellList : Item;
var cnt : int;
var StoreInventory1 = new List.<Item>();

var my_item : Item; //= new Item("posion",360);
var my_item1 : Item; //= new Item("posion",360);

function Start() {

So as you can see the storeInventory list should be populated with everything contained in the StoreInventory1 list in Items.js
However, I am getting an Argument is out of range exception on the line I marked in the Store.js script. Can anyone take a quick look and see what I might be doing wrong? Thank you!

What is the “x” variable? It’s being set in the inspector, so apparently you have it greater than the length of the StoreInventory1 list. Instead of setting the limit manually like that, just use the count of the list; that way it would be impossible to be out of range.