Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by AlexGan001 · May 23, 2016 at 03:17 PM · arraycheck

How to check for an empty array

I have been working on a game for a while, but unity keeps giving me unnecessary error messages which don't effect the function of my game a tall. I have found the problem.

It occurs on this line of code:

 if (Movables[i].Right.movableBlock.Length > 0)

When the array Movables is empty and has nothing in any of it's slots.

The error keeps occurring, yet the code works perfectly, so I just need a way to code around the error. Perhaps somehow check for an empty array instead of checking in slot 0 which has nothing in it (the likely cause). I don't know how to do this, so a simple solution would be great.

The error states 'Argument is out of range'

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 ninja_gear · May 23, 2016 at 04:35 PM 0
Share

When working with arrays, safety checks are a good practice.

     if ($$anonymous$$ovables != null && $$anonymous$$ovables.Length > 0)
     {
      if ($$anonymous$$ovables[i] != null && $$anonymous$$ovables[i].Right != null)
       {
        if ($$anonymous$$ovables[i].Right.movableBlock != null &&$$anonymous$$ovables[i].Right.movableBlock.Length > 0)
        {
          //do stuff
        }
       }
      }

1 Reply

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

Answer by tanoshimi · May 23, 2016 at 03:20 PM

It's unclear what value you're supplying for i, but if it's an iterator, you should only be increasing it up the maximum length of Movables -1:

 for(int i=0; i< Movables.Length; i++){
    if (Movables[i].Right.movableBlock.Length > 0)
   ... do whatever
 }
Comment
Add comment · Show 10 · 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 AlexGan001 · May 23, 2016 at 03:22 PM 0
Share

I am not using a loop within my code.

avatar image AlexGan001 AlexGan001 · May 23, 2016 at 03:25 PM 0
Share

What would really help is if you could give me a line of code which says:

If there is nothing in the array called 'movableBlock' that doesnt create this error

avatar image tanoshimi AlexGan001 · May 23, 2016 at 04:04 PM 0
Share

Your error is caused by the logic in the line immediately above:

 if ($$anonymous$$ovables [i].Right.fixedBlockPresent || $$anonymous$$ovables [i].targetPos != Vector3.zero) {
   i = $$anonymous$$ovables.Count;
 }

You're setting i = $$anonymous$$ovables.Count, and then trying to access $$anonymous$$ovables[i].

Array indexers are zero-based, so if $$anonymous$$ovables.Count has, say, 20 elements, then you can access $$anonymous$$ovables[0] - $$anonymous$$ovables[19], not $$anonymous$$ovables[20].

If what you were trying to do was set i to the last element of $$anonymous$$ovables, that line should have read: if ($$anonymous$$ovables [i].Right.fixedBlockPresent || $$anonymous$$ovables [i].targetPos != Vector3.zero) { i = $$anonymous$$ovables.Count - 1; }

avatar image tanoshimi AlexGan001 · May 23, 2016 at 03:28 PM 0
Share

If you're not using a loop, then you really need to add some logic at the point you set i, to ensure that it's not beyond the size of the $$anonymous$$ovables array.

avatar image AlexGan001 tanoshimi · May 23, 2016 at 03:34 PM 0
Share

Sorry, the statement is actually deeply contained in a loop that I didn't spot. But the 'i' is not the issue. I am checking the other array 'movableBlock', and not '$$anonymous$$ovables', for it's length.

Show more comments
Show more comments
avatar image Dave-Carlile AlexGan001 · May 23, 2016 at 04:21 PM 2
Share
  i = $$anonymous$$ovables.Count;

That line is your problem. On the next line you're accessing the array using that index which is 1 beyond the total number of items in the array. Why are you doing that? If you want to break out of the loop then use break.

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

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

how do i check if all booleans in an array are false? C# 2 Answers

Check rotation for a Rigidbody2D 0 Answers

Assigning a GameObject variable to equal another GameObject variable via C# script. 0 Answers

method to display random question in C# 3 Answers

Array sorting issues - Please Help!!! 4 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