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 ramjigeddam · Nov 19, 2013 at 07:32 AM ·

Array index is out of range i am getting this error Please help me

var i:int;

var move:GameObject[];

move=GameObject.FindGameObjectsWithTag("Player");

var screenPoint:Vector3;

var offset:Vector3;

var curScreenPoint:Vector3;

var curPosition:Vector3;

function OnGUI()

{ if(GUI.Button(Rect(0,60,50,30),"Explore")) { for(i=0;i<=move.Length;i++)

 {
 screenPoint = Camera.main.WorldToScreenPoint(move[i].gameObject.transform.position);
 offset = move[i].gameObject.transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
 

//function OnMouseDrag() //{ curScreenPoint = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint); transform.position = curPosition;

//} } }

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 meat5000 ♦ · Nov 19, 2013 at 08:46 AM 0
Share

Try taking out the = sign in

  i<=move

3 Replies

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

Answer by hirenkacha · Nov 19, 2013 at 09:32 AM

Array's length is always the last index +1. If you try accessing the last element of an array it should be [Length-1] and NOT [Lenght]. For Eg. For an array 'a' having 10 elements, a.Length = 10;

 a[0] = ..  //Length=1
 a[1] = ..  //Length=2
 .
 .
 a[9] = ..  //Length=10
 a[10] = null

So you need to change your for loop to

 for(i=0;i<move.Length;i++)

this will solve the problem.

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 ramjigeddam · Nov 19, 2013 at 09:36 AM 0
Share

Thanks for all

avatar image
0

Answer by KiraSensei · Nov 19, 2013 at 08:55 AM

Try to loop like that :

 for (int i=0; i<move.Length; i++)

You are going too far.

Your tags are useless, it exists to make categories of questions, and by definition anyone who asks a question needs help.

And please next time give the error line and properly format your code, some people who can help you won't just because it is unreadable.

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 Hoeloe · Nov 19, 2013 at 09:00 AM

You're not quite understanding how arrays are structured. An IndexOutOfRange exception is thrown when you try to access part of an array that does not exist. For example, if I have an array with 5 elements in it, and I try to access the 6th, it will give me the OutOfRange exception, because there is no 6th element.

Now, you also need to consider that arrays are what we call 0-indexed. This means that, when accessing an array, to get the first element in the array, you ask for index 0 (that is, array[0] will give you the first element. This has some implications that you haven't quite picked up on, that I will try and explain with an example. Let's say I have an array of length 6 (that means there are 6 elements in the array, and array.Length is equal to 6). It looks like this:

 { a, b, c, d, e, f }

Now, I'll annotate this with the index you need to call to access the specific element of the array:

 i =   0  1  2  3  4  5
     { a, b, c, d, e, f }

So if we call array[i] we will get the corresponding item in the array when i is any of the values seen there. The implication here is something you might have spotted already. The array contains 6 elements. The length of the array is 6. But, because the first element is indexed with 0, the last is indexed with 5, not 6. Zero indexing means the last element of an array can be gained like this: array[array.Length-1].

Now, let's have a look at your for loop:

 for(i=0;i<=move.Length;i++)

Now, this means that i will progress from 0 to the length of the array move, inclusive. Let's assume move has 3 elements. On the first run on this loop, i will be 0, so calling move[i] equates to move[0], which, as we've said, is the first element of the array.

On the next iteration, move[i] equates to move[1], which is the second element.

On the third iteration, move[i] becomes move[2], which is the third element of the array.

Now, at the start of the next iteration, i is equal to 3, so we check the condition: i<=move.Length, and find that 3move[i] becomes move[3], which gets the fourth element of the array... but the array only has 3 elements, so there is no fourth element to get. This causes the program to throw and OutOfRange exception, to prevent you from accessing uninitialised spaces in memory.

Hopefully this has helped you to understand what is causing the problem, and if you've read this through properly, you'll be able to fix it.

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

21 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

Related Questions

Multiple Cars not working 1 Answer

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer

help my unity wont start up when i try every thing but the game options gos white then when i go back to it later the white turns black 0 Answers

Is making a game hard without a team 4 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 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