Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Mimumu84 · Sep 07, 2014 at 04:47 PM · variablesinventory

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;
 }
Comment
Add comment · Show 8
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 dsada · Sep 07, 2014 at 04:55 PM 0
Share

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...

avatar image AyAMrau · Sep 07, 2014 at 05:00 PM 0
Share

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.

avatar image Mimumu84 · Sep 07, 2014 at 05:54 PM 0
Share

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".

avatar image AyAMrau · Sep 07, 2014 at 06:02 PM 3
Share

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.

avatar image dsada · Sep 07, 2014 at 06:25 PM 3
Share

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 :)

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Inventory Cursor Location 1 Answer

Weird issues when adding an object to a variable. 1 Answer

How to determine what type of class is being interated through in an array of multiple classes 1 Answer

How to implement pickups that can go in a inventory 3 Answers

Text RPG inventory 1 Answer


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