- Home /
Index out of range exception
I cannot seem to find the array problem in my script below even though the console shows that the error is at line 35. Furthermore, I have used some similar functions from design3 inventory system tutorial with little modifications which could cause the error. I've tried converting my "items" array variable into a public variable and it doesn't work:
var maxSlots : int;
var invParent : Transform;
var minSlots : int = 0;
var newItem : Transform;
var noItems : int;
var full : boolean;
var itemSelected : Transform;
var throwTime : float;
var style : GUISkin;
var openInv : boolean;
var windowRect : Rect = Rect(20, 20, 120, 50);
var slotsize : int;
var spacing : int;
var tempTex : Texture;
var defaultTex : Texture;
var pressed : boolean;
var items : GameObject[];
var dragobject : GameObject;
var buttonClicked : boolean = false;
var objAttached : boolean = false;
var lastSlot : int;
var slotMovedFlag : boolean = false;
function OnGUI() {
GUI.skin = style;
if(openInv) {
windowRect = GUI.Window(0, windowRect, DoMyWindow, "Inventory");
}
}
function DoMyWindow (windowID : int) {
for(var i = 0; i < 4; i++) {
for(var j = 0; j < 4; j++) {
if(items[16 + (4 * i) + j - 21] == null)// error is here {
tempTex = defaultTex;
}
else {
tempTex = items[16 + (4 * i) + j - 21].GetComponent("itemStats").itemSlottex;
}
if(Input.GetMouseButtonDown(0)) {
SetButtonClicked(false);
}
if(GUI.Button(Rect(slotsize * j + spacing * (j+1), slotsize * i + spacing * (i+1) + 15, slotsize, slotsize), tempTex)) {
SetButtonClicked(true);
if(items[16 + (4 * i) + j - 21] != null) {
dragobject.SetCursor(items[16 + (4 * i) + j - 21].GetComponent("itemStats").itemSlottex);
SetObjAttached(true);
lastSlot = 16 + (4 * i) + j - 21;
}
MoveSlot(16 + (4 * i) + j - 21);
}
}
}
}
function MoveSlot(indSlot : int) {
if(items[indSlot] == null && !GetSlotMovedFlag()) {
SetObjectInBag(indSlot, items[GetLastSlotUsed()]);
ReleaseSlot(lastSlot);
SetSlotMovedFlag(true);
dragobject.getComponent("Drag").ReleaseCursorIcon();
SetObjAttached(false);
}
else if(items[indSlot] != null && items[GetLastSlotUsed()] != null && !GetSlotMovedFlag()) {
var tmpObj : GameObject = GetObjectsInBag(indSlot);
SetObjectInBag(indSlot, items[GetLastSlotUsed()]);
SetObjectInBag(lastSlot, tmpObj);
SetSlotMovedFlag(true);
dragobject.getComponent("Drag").ReleaseCursorIcon();
SetObjAttached(false);
}
else {
SetSlotMovedFlag(false);
}
SetLastSlotUsed(indSlot);
}
function ReleaseSlot(index : int) {
SetObjectInBag(index, null);
}
function AddItem() {
if(full == false) {
newItem.parent = invParent;
noItems++;
items.Add(newItem);
}
}
function RemoveItem() {
var script : itemStats = itemSelected.gameObject.GetComponent("itemStats");
script.useable = false;
yield WaitForSeconds(throwTime);
itemSelected.parent = null;
noItems--;
}
function Update() {
if(noItems >= maxSlots) {
full = true;
}
else {
full = false;
}
if(noItems <= minSlots) {
noItems = minSlots;
}
if(Input.GetKey("i") && openInv == false && pressed == false) {
openInv = true;
wait();
}
else if(Input.GetKey("i") && openInv == true && pressed == false) {
openInv = false;
wait();
}
}
function wait() {
pressed = true;
yield WaitForSeconds(0.3);
pressed = false;
}
function GetObjectsInBag(t : int) {
return items[t];
}
function SetObjectInBag(t : int, obj : GameObject) {
items[t] = obj;
}
function SetLastSlotUsed(t : int) {
lasSlotUsed = t;
}
function GetLastSlotUsed() {
return lastSlot;
}
function SetSlotMovedFlag(val : boolean) {
slotMovedFlag = val;
}
function GetSlotMovedFlag() {
return slotMovedFlag;
}
function GetButtonClicked() {
return buttonClicked;
}
function SetButtonClicked(val : boolean) {
butonClicked = val;
}
function GetObjectAttached() {
return objAttached;
}
function SetObjAttached(val : boolean) {
objAttached = val;
}
What does that formula (16 + (4 * i) + j - 21) means? how did you create it?
Just another note: You should really think about make more classes that is responsible for smaller tasks because this is already a class that is can not be decrypted...
items[16 + (4 * i) + j - 21] - Even if this works in some case, it just looks like asking for trouble. What are you trying to achieve here, is there not an easier way?
Anyway if you need to do calculations to find the index you should save the result to a variable first and check that it's in range 0 to (Array.Length - 1). Then if it's not you can easily investigate the value and see where it went wrong.
the formula helps me assign my gui buttons with each of my gameobjects in the array. dsada, can u explain to me about making more classes because I'm not really good with classes. And I dont quite understand what you mean by "class that is cannot be decrypted".
I think you need to rethink this formula, because for example when i is 0 the result will always be less than 0:
When i = 0 && j = 3 Then 16 + (4 * 0) + 3 - 21 = 19 - 21 = -2
Either you want to ignore results like that - then put a check on the value beforehand and just use the valid numbers; or you want to always have a correct result, then the formula is incorrect.
Well i meant that this class is just too complicated. You should try to separate Inventory logic and Inventory UI into different classes. This is the object oriented program$$anonymous$$g and $$anonymous$$odel/View architecture. You should read after it.
Anyways to stick with your problem. If you just want to iterate through your 1 dimensional array with a double loop the formula is bad. the right formula is items[i*4 + j]. I think you were trying to create this one :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613162519im_/https://answers.unity.com/themes/thub/images/avi.jpg)