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
1
Question by sillyrabbit · Apr 22, 2012 at 02:50 AM · arrayrangelengthoutof

Array out of its own range?

 var farCell = cell.Length;
     var middle : Vector3;
          middle = cell[farCell].transform.position;

I'm trying to get the transform position of the last element of the array. It's giving me an out of range error. If I negate cell.length by one, I no longer get an error. Suggesting the length of the array is one greater than the length of the array...

Any ideas?

Thanks guys

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 bodec · Apr 22, 2012 at 03:09 AM 1
Share

are you accounting for the array counts 0,1,2,3,4 and so on?

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Kleptomaniac · Apr 22, 2012 at 03:38 AM

Yes, this is because of the structure of arrays. Let me explain.

Say you were to have an array of ints called intArray which contains [5, 10, 15, 20, 25, 30, 35]. The way in which array elements are structured is such that the first element's index is not 1, but 0! From there, indexes continue in consecutive order like so: 0, 1, 2, 3, 4 .. etc.

Therefore, in the case of our example, intArray[0] = 5, intArray[1] = 10, intArray[2] = 15 ... all the way up to intArray[6] = 35. However, the .Length property of an array works as you would expect, in that it returns the number of elements in the array, not any of the indexes. Therefore, since there are 7 elements in intArray, intArray.Length returns 7.

Here's where it gets really trippy however; if you are trying to return the last element of the array (like you are trying to do), you would of course use intArray.Length like you have done. However, if you try to use intArray[intArray.Length], this will return an out of range error.

In simplest terms that is because intArray.Length = 7, and there is no intArray[7] in intArray! The largest element is of index 6 because of the inclusion of an element 0 in the array. Therefore, intArray.Length - 1 is required in order to return intArray[6]. :)

Hope that made sense! Klep

Comment
Add comment · Show 3 · 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 Kleptomaniac · Apr 22, 2012 at 03:39 AM 0
Share

Haha, $$anonymous$$e didn't turn out to be very simple! :D

avatar image bodec · Apr 22, 2012 at 03:42 AM 0
Share

lol yes but your explaination deserves a thumbs up.

avatar image Kleptomaniac · Apr 22, 2012 at 03:45 AM 0
Share

Aww, thanks. :)

avatar image
0

Answer by AchillesWF · Apr 22, 2012 at 03:37 AM

The Length is the number of items in your array, which are indexed from 0 to the number of items minus 1. So, using farCell is out of range.

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
0

Answer by Blackjackjulian · Apr 22, 2012 at 03:36 AM

Cell.length returns the number of elements in the array.

But the elements are numbered from 0 onward.

You're getting element 9 in a 9 element array. But they're numbered 1-8.

Element.length-1.

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
0

Answer by aldonaletto · Apr 22, 2012 at 03:33 AM

Like @bodeci said, arrays start at 0, thus the last element is cell.length-1 - cell.length is out of range.

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

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

8 People are following this question.

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

Related Questions

Array index is out of Range!? 1 Answer

Wierd Animation Bug 0 Answers

Why is this array out of length? 1 Answer

Array out of Range 1 Answer

Random object, Array index out of range 0 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