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 Theacesofspades · Apr 06, 2013 at 03:28 AM · javascriptarraysinventorygui.button

Why does it give me this error and how can i fix it?

So im attempting to learn how to use for loops and arrays together but i cant seem to figure out this error it keeps giving me. The error says "IndexOutOfRangeException. Array index is out of range". The error is on line 52, the one i put stars next to. The script im using is:

 var numberInventory : int[];
 private var number : int = 0;
 
 var showInventory = false;
 
 var xStart : float;
 var yStart : float;
 
 var buttonSize = 80;
 
 
 
 
 function Start () 
 {
     numberInventory = new int[number];
     
     xStart = Screen.width/2-200;
     yStart = Screen.height/2-200;
 }
 
 function Update () 
 {
     if (Input.GetKeyDown("e"))
     {
         if (!showInventory)
         {
             showInventory = true;
         }else{
             showInventory = false;
         }
     }
 }
 
 function OnGUI ()
 {
     if (GUI.Button(Rect(0,0,100,100), "ADD"))
     {
         number ++;
         numberInventory = new int[number];
     }
         
     if (showInventory)
     {
         
         GUI.Box(Rect(xStart, yStart-20, 400, 420), "Inventory");
         
         for (var x=0; x<5; x++)
         {
             for (var y=0; y<5; y++)
             {
                 ****if (GUI.Button(Rect(xStart+buttonSize*x, yStart+buttonSize*y, buttonSize, buttonSize), ""+numberInventory[number]))****
                 {
                     
                 }
             }
         }
     }
 }

How can i fix this? Thanks!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Chronos-L · Apr 06, 2013 at 04:07 AM

Index problem.

A array with size n will have n elements and the indexes of these elements are { 0, 1, ... n-1}.

You declare your array as:

  numberInventory = new int[number];

So the index should be in { 0, 1, ..., number - 1 }. Using numberInventory[number] will produce a index out of range exception.

 var index = 0;
 for (var x=0; x<5; x++)
 {
    for (var y=0; y<5 && index < number; y++)
    {
       if (GUI.Button(Rect(xStart+buttonSize*x, yStart+buttonSize*y, buttonSize, buttonSize), ""+numberInventory[index++]))
       {
  
       }
     }
  }
Comment
Add comment · Show 2 · Share
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 Theacesofspades · Apr 06, 2013 at 04:17 PM 0
Share

Wait so what is the index of an array?

avatar image Chronos-L · Apr 07, 2013 at 01:41 AM 0
Share

Usually in program$$anonymous$$g, zero-based indexing is used.

Index can be see as the offset of the memory. In memory, arrays are stored as a large block of memory, each smaller block is an element and their have a definite size.

  [   ][   ][   ][   ][   ][   ]...

In C, we refer to an array using pointer. Lets say the location of a int array in the memory is at 0F4A (random hexadecimal), then the first value will be at OF4A, the second value will be at ( OF4A + 1 memory-size-of-int ), and the third value will be at ( 0F4A + 2 memory-size-of-int ), so on and so forth. The shorthad way of writing that would be arr[x] where x is the offset of the memory pointer.

Another way to see it is, there are 10 person standing in a line and they are 1 meter apart from each other. The first person will stand at X, then the second person will be standing at X + 1, the nth person would be standing at X + (n-1).

So the first element is arr[0], the second element is arr[1], so on and so forth. You can see this in arithmetic progression as well: a + (n-1)d, when you are the first one, n = 1, so you will get a + ( 1 - 1 )d, which is a.

This all may seems counter-intuitive, because in daily life we count in { 1, 2 ,3 .... }, but it will make more sense when you get used to it.

http://en.wikipedia.org/wiki/Zero-based_numbering

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Inventory Help. 2 Answers

Where can i begin to learn how to make an inventory. (Javascript). 1 Answer

Saving Certain Vector3's positions 2 Answers

I'm trying to add items to an 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