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 Punkjim420 · Jan 24, 2013 at 12:02 AM · javascriptlistsizeout of range

whats this Argument Out Of Range error when navigating between 0 and max value?

Im making a combat menu like this:

<-- Skills: skill name -->
<-- Magics: magic name -->
<-- Items : item name -->

skill name's variable is called skillbagCursor
magic name's variable is called magicbagCursor
item name's variable is called itembagCursor

pressing left or right arrow should change the value in the targetted horizontal row. that works, but when i go left or right i get an error, i used a Debug.Log(Master.itembag.Count); to see the size of the list and it always say 2 or more, it never says 1 or 0 though I add 3 total items at 3 seperate times(should say 1, then 2, then 3).. im stumped. Would anyone like to help me figure this out?

 import System.Collections.Generic;
 
 var firstPlay : boolean = true;
 var itembagCursor : int = 0;
 static var itembagCursorMax : int = 0;
 var skillbagCursor : int = 0;
 static var skillbagCursorMax : int = 0;
 var magicbagCursor : int = 0;
 static var magicbagCursorMax : int = 0;
 static var selectedSkill : String;
 static var selectedMagic : String;
 static var selectedItem : String;
 static var activeCursor : String = "skillbag";
 
 function Start(){
 
     if(firstPlay){
         Master.itembag.Add("Empty"); //add item to inventory
         Master.skillbag.Add("Rush Cut"); 
         Master.magicbag.Add("Fire");
         firstPlay = false;//save this value on saving!!!***~~~
     }
 }
 
 function Update(){
 itembagCursor = Mathf.Clamp(itembagCursor, 0, itembagCursorMax);
 itembagCursorMax = Master.itembag.Count;
 skillbagCursorMax = Master.skillbag.Count;
 magicbagCursorMax = Master.magicbag.Count;
 selectedItem = Master.itembag[itembagCursor];
 selectedMagic = Master.magicbag[magicbagCursor];
 selectedSkill = Master.skillbag[skillbagCursor];
 Interface.messageWindow = selectedItem;
 /* SET THE ACTIVE CURSOR */
 
 if(Input.GetKeyDown(KeyCode.UpArrow) == true)
 {
     switch(activeCursor) 
     {
         case "skillbag":
             activeCursor = "itembag";
             break;
         case "magicbag":
              activeCursor = "skillbag";
              break;
         case "itembag":
             activeCursor = "magicbag";
             break;
     } 
 } 
 
 else if(Input.GetKeyDown(KeyCode.DownArrow) == true)
 {
     switch(activeCursor) 
     {
         case "skillbag":
             activeCursor = "magicbag";
             break;
         case "magicbag":
              activeCursor = "itembag";
              break;
         case "itembag":
             activeCursor = "skillbag";
             break;
     } 
 } 
     
 /* ENABLE ACTIVE CURSOR TO NAVIGATE ITEMS <- & -> */
 
     if(Input.GetKeyDown(KeyCode.LeftArrow) == true)
     {
         switch(itembagCursor) 
         {
            case itembagCursor > 0:
                 itembagCursor = itembagCursor - 1;
                 break;
             case itembagCursor == 0:
                  itembagCursor = itembagCursorMax;
                  break;
         } 
     }
     
     if(Input.GetKeyDown(KeyCode.RightArrow) == true)
     {
         switch(itembagCursor < itembagCursorMax) 
         {
            case true:
                 itembagCursor = itembagCursor + 1;
                 break;
             case false:
                  itembagCursor = 0;
                  break;
         } 
     }
     
     if(Input.GetKeyDown(KeyCode.LeftArrow) == true)
     {
         switch(magicbagCursor) 
         {
            case magicbagCursor > 0:
                 magicbagCursor = magicbagCursor - 1;
                 break;
             case itembagCursor == 0:
                  magicbagCursor = magicbagCursorMax;
                  break;
         } 
     }
     
     if(Input.GetKeyDown(KeyCode.RightArrow) == true)
     {
         switch(magicbagCursor) 
         {
            case magicbagCursor < magicbagCursorMax:
                 magicbagCursor = magicbagCursor + 1;
                 break;
             case magicbagCursor == magicbagCursorMax:
                  magicbagCursor = 0;
                  break;
         } 
     }
     
     if(Input.GetKeyDown(KeyCode.LeftArrow) == true)
     {
         switch(skillbagCursor) 
         {
            case skillbagCursor > 0:
                 skillbagCursor = skillbagCursor - 1;
                 break;
             case itembagCursor == 0:
                  skillbagCursor = skillbagCursorMax;
                  break;
         } 
     }
     
     if(Input.GetKeyDown(KeyCode.RightArrow) == true)
     {
         switch(skillbagCursor) 
         {
            case skillbagCursor < skillbagCursorMax:
                 skillbagCursor = skillbagCursor + 1;
                 break;
             case skillbagCursor == skillbagCursorMax:
                  skillbagCursor = 0;
                  break;
         } 
     } 
 //USE THE ITEMS WHEN ENTER PRESSED IN COMBAT MENU//
 
     if(Input.GetKeyDown(KeyCode.Return) && activeCursor == "itembag" && selectedItem == "Potion" && Master.potion > 0){
         Statistics.playerCurHp = Statistics.playerCurHp + 100;
         Interface.messageWindow = "You used a 'Potion'.";
         Master.potion = Master.potion - 1;
     }
     if(Master.potion == 0 && Master.itembag.Count > 1){
         
         Master.itembag.Remove("Potion");
     }
     if(Master.potion == 0 && Master.itembag.Count == 1){
         Master.itembag.Add("Empty");
         Master.itembag.Remove("Potion");
     }
     if(Input.GetKeyDown(KeyCode.Return) && activeCursor == "itembag" && selectedItem == "Ether" && Master.ether > 0){
         Statistics.playerCurMp = Statistics.playerCurMp + 50;
         Interface.messageWindow = "You used an 'Ether'.";
         Master.ether = Master.ether - 1;
     }
     if(Master.ether == 0 && Master.itembag.Count > 1){
         Master.itembag.Remove("Ether");
         
     }
     if(Master.ether == 0 && Master.itembag.Count == 1){
         Master.itembag.Add("Empty");
         Master.itembag.Remove("Ether");
     }
 }
Comment
Add comment · Show 1
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 Punkjim420 · Jan 24, 2013 at 12:34 AM 0
Share

by the way, the error i get is here at thie comment message/selectedItem
function Update(){
itembagCursor = $$anonymous$$athf.Clamp(itembagCursor, 0, itembagCursor$$anonymous$$ax);
itembagCursor$$anonymous$$ax = $$anonymous$$aster.itembag.Count;
skillbagCursor$$anonymous$$ax = $$anonymous$$aster.skillbag.Count;
magicbagCursor$$anonymous$$ax = $$anonymous$$aster.magicbag.Count;
selectedItem = $$anonymous$$aster.itembag[itembagCursor]; //ERROR HERE
selected$$anonymous$$agic = $$anonymous$$aster.magicbag[magicbagCursor];
selectedSkill = $$anonymous$$aster.skillbag[skillbagCursor];
Interface.messageWindow = selectedItem;

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Dave-Carlile · Jan 24, 2013 at 12:53 AM

It looks like you're setting the max itembagCursor to the count of items. Array's are relative to 0, so valid indexes for Master.itembag are 0 to Master.itembag.Count - 1.

Try this:

 itembagCursorMax = Master.itembag.Count - 1;

Also, shouldn't you move that line above the clamp?

Comment
Add comment · Show 6 · 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 Punkjim420 · Jan 24, 2013 at 01:36 AM 0
Share

ill try this and see. Thank you for your reply.

avatar image Punkjim420 · Jan 24, 2013 at 01:56 AM 0
Share

It worked like a charm i cant believe i missed that! I find i miss a lot of behind the scenes stuff... thanks a lot.

avatar image sparkzbarca · Jan 24, 2013 at 05:16 AM 1
Share

also you probably shouldnt use clamp. Clamp is for floats really.

if you want to make sure its equal to 0 1 or 2 use the modulo operator and just keep adding. $$anonymous$$odulo is the remainder left after deviding. So

number % 3 = 0

number % 3 = 1

number % 3 = 2

there are no other options. A number divided by 3 can either have a remainder of 1, a remainder of 2 or if there is 3 more it goes into it one more time gonig into it perfectly and there is no remainder.

so for yours for example

if there are 4 choices, or more generally Item$$anonymous$$axSize number of choices.

the choice your on is

(Cursor % Cursor$$anonymous$$ax)

assu$$anonymous$$g the cursor starts at 0

and cursormax is 4 for example

4 goes into 0, 0 times and the remainder is 0

cursor++;

4 goes into 1, 0 times and the remainder is 1

cursor++

4 goes into 2, 0 times and the remainder is 2.

corusor++

4 goes into 3, 0 times and the remainder is 3.

cursor++

4 goes into 4, 1 time and the remainder is 0.

4 goes into 5, 1 time and the remainder is 1.

as you can see we have created an infinite loop whereby we only arrive at one of 4 points and by setting it to cursor$$anonymous$$ax we can change the value later to account for new items or subtract to remove them.

avatar image Punkjim420 · Jan 24, 2013 at 09:03 AM 0
Share

Cool. Thats understandable. Ill change the Clamp into a $$anonymous$$odulo as soon as i fix my current issue... same error as above this time with items being used. Should i make a new question for the same script, same issue, but at different lines?

EDIT: On second thought, i ran a check with printing my inventory size, and my amount of items dont -- inventory size. I think i found a great lead to solve it myself.

thank you for all of your help @sparkzbarca

avatar image sparkzbarca · Jan 24, 2013 at 04:20 PM 0
Share

inventory size and accessing an element of an inventory arent the same thing because of the 0 difference.

an inventory with 1 item has a size of 1

that item is not however inventory[size];

or inventory[1]

its inventory[size -1]

or inventory[0]

thats because the FIRST item is stored in the ZERO slot. likewise the last item is stored in the last-1 slot and in general the iterator to access any element is its number -1;

you need that because you cant say a list with 0 elements is size 0 and also say a list with one elements is size 0. It has to be size 1

however starting from 0 with arrays is needed because an array of size 0 is techinically size 1

an empty array contains 1 item. its a special item that denotes the end of the list. strings have this as well a character that denotes the end of the string. That allows the computer to know when the string or list ends and know its size and when to stop iterating.

Show more comments

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

A node in a childnode? 1 Answer

List of type Button has size set to zero at runtime 0 Answers

Problem declaring List variable in JS 2 Answers

Java Script command list 3 Answers

FindIndex in a List 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