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 Fewpwew130 · Oct 23, 2014 at 05:59 AM · arrayout of range

Array index is out of range

Hello, I am new to arrays. I want to have an array containing 3 float values, which I am able to declare. The console gives error for the last line: "Array index is out of range".

using UnityEngine; using System.Collections;

 public class ChangeArray : MonoBehaviour {
 
 //public    string[] myArray = new string[2];
 public    float[] myArray2 = new float[2];
 
 
 
     // Use this for initialization
     void Start () {
     //    myArray[0] = "first value";
     //    myArray[1] = "second value";
     //    myArray[2] = "third value";
         myArray2[0] =  5f;
         myArray2[1] = 10f;
         myArray2[2] = 15f;
 
     }
 }
 
 
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

2 Replies

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

Answer by robertbu · Oct 23, 2014 at 06:05 AM

The size of your array is 2, but you are putting three values in it. This line:

 public float[] myArray2 = new float[2];

...creates an array with 2 slots (0 and 1). Note that since the array is public, changing the 2 to 3 will not fix the problem. You need to either make the change in the Inspector, change the size and in the Inspector use the Reset for this object, or change the value and make the array private. I vote for the last if it doesn't interfere with something you have planned.

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 Fewpwew130 · Oct 23, 2014 at 06:08 AM 0
Share

Robertbu, thanks, I tried changing 2 to 3 in inspector and error disappered! But why did it happen? Should I always change it via inspector, when creating an array? :D

avatar image robertbu · Oct 23, 2014 at 06:35 AM 0
Share

With a public class instance variable, the initialization happens at the time the script is attached to the object. Changes to the initialization code after the script is attached are ignored. If you look on the right hand side of any component on a game object in the Inspector, you will see a gear icon. Clicking on the gear icon will show a dropdown that includes a 'Reset' option. Selecting this option will cause the component to reinitialize the component. So you could have made the change in script and then use 'Rest'.

But unless you have a reason for making it public, the best solution is to just make it private:

  private float[] myArray2 = new float[3];

Any changes you make to initialization of private variables will happen when the script is recompiled.

avatar image Dark_Tiger · Oct 23, 2014 at 06:37 AM 0
Share

It happened because you were trying to put 3 people in a 2 person car, as an analogy. You have to create the array with however many objects you want to store in it, so you're storing 3 items in your array it has to be [3] if you were storing 100 items in the array it would have to be [100] so on

avatar image
0

Answer by Fewpwew130 · Oct 23, 2014 at 06:31 PM

Thank you guys for help!

Robertbu, I named it public, because I was not quite sure what the difference and limitation of private was, apart from being hidden from the inspector. I thought private variables could only be accessed inside a single script and therefore could not be got by using .GetComponent script, to get the value for another script. :D

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

27 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

Related Questions

Array index out of range after build 1 Answer

Array out of index? 1 Answer

How do I check a List[0], when it's nothing? 1 Answer

How to check if inspector-filled array is empty 0 Answers

Out of range error 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