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 derrtyones · Aug 29, 2012 at 10:38 AM · javascriptarraynullindexlength

Array error - Index is less than 0...

Hi,

I have an array. Whenever I add something to that array, it keeps giving me the following error:

ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.

What causes this and how can I fix this? I searched through similar answers but I still can't get mine to work. Most other users have a for loop and I don't use one.

The thing is that I need to do something when [0] and [1] are filled. So I check if [0] is empty and then execute a code.

 var playerInventory = new Array ();
 
 function Update() {
 
     if(playerInventory.length > 0){
         if(playerInventory[0] != null){
             // do something
         }
         
         if(playerInventory[1] != null){
             // do something
         }
     }
     
     for (var value : String in playerInventory) {
         print(value);
     }
 }
 
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

3 Replies

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

Answer by derrtyones · Aug 29, 2012 at 02:12 PM

It works fine now when I check it with:

if(playerInventory.length == 1)

and do that for each element. It does not matter for this array as it will get a max amount of items.

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

Answer by lvictorino · Aug 29, 2012 at 11:39 AM

When you test `playerInventory.length`, the fact that it returns more than 0 does not mean that it actually contains 2 entry. So when you want to know what is filled in `playerInventory[1]` (the second index actually) you can't be sure it actually exists.

Does it work better with : `if(playerInventory.length >= 2)` ?

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 derrtyones · Aug 29, 2012 at 12:05 PM 0
Share

It works slightly better, but it's not solved yet. When I hit the first box to add something into that array, no errors are shown. When I hit the 2nd box (2nd data is added to the array) the same error pops up. But then when I hit the third box to add a third piece of data to the array, the error is gone. How do I fix this?

avatar image lvictorino · Aug 29, 2012 at 12:18 PM 1
Share

Can you please paste us how you "add" something to your array ?

avatar image derrtyones · Aug 29, 2012 at 12:28 PM 0
Share

gameObject.Find("itemHolder").GetComponent("playerInventory").playerInventory.Push(gameObject.tag);

avatar image lvictorino · Aug 29, 2012 at 12:32 PM 1
Share

And when you display playerInventory.Length what do you have ?

avatar image derrtyones · Aug 29, 2012 at 12:35 PM 0
Share

When I start the game it says 0. When I pick up all 3 items (by touching the boxes) it says 3.

Show more comments
avatar image
0

Answer by speedything · Aug 29, 2012 at 12:18 PM

Would a list work better? As arrays are of a fixed size, they are always the same length - regardless of what they contain.

If you do want to keep using an array then I don't think you want to be checking the length, and you'd be better off just checking the individual items. So...

 function Update()
 {
    if ((playerInventory[0] != null) && (playerInventory[1] != null))
    {
       // Do what you want to do here
    }
 }
Comment
Add comment · Show 1 · 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 derrtyones · Aug 29, 2012 at 12:26 PM 0
Share

The suggestion to not check for the length, but to only check for individual items ins$$anonymous$$d is nice, but when I do that, I still get the error: Index is less than 0 or more than or equal to the list count.

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

9 People are following this question.

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

Related Questions

Null first array index and shift rest by 1? 1 Answer

Static Array - Throwing NullReference 1 Answer

Why is my array null? 0 Answers

Checking array fails each time. But change anything and resave and it works until unity closes...... 1 Answer

Javascript array: Negative Indexes? 2 Answers


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