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 lmstam · Oct 01, 2015 at 12:03 PM · c#arrayarray-out-of-range-except

Array index is out of range?

I have this error and I'm not sure why it occurs. It says IndexOutOfRangeExcepton - Array is out of range.

Here's my code: (it's not the whole code but I'll only show the relevant code)

 public int i = 0;
 public Plant[] carrotArray;


 // Use this for initialization
 void Start () {
     carrotArray = new Plant[i];
     carrotArray[i].GetComponent<SpriteRenderer>().sprite = CS0;
 }

  public void CarrotClicked ()
 {
     if (amountCarrots > 0 && amountTurns > 0) {
         carrotArray[i] = Instantiate (carrotArray[i], pos, Quaternion.identity) as Plant;
         i++;            
     }
 }

And the error references to: carrotArray[i].GetComponent().sprite = CS0; and carrotArray[i] = Instantiate (carrotArray[i], pos, Quaternion.identity) as Plant;

I don't get why this happens, can anyone help me?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Suddoha · Oct 01, 2015 at 01:51 PM

You have to make sure that the array is properly populated. Either in the inspector or through a script.

Currently, you create a new array in start with 'i' elements, while 'i' is 0 at the time (if you didn't change it in the inspector). So you'll have an array with 0 elements. Even if you populated it through the inspector, this wouldn't have any effect as you have that one line in the Start method which re-assigns the array and does not populate it.

Also, the code you've posted may sooner or later run into another IndexOutOfRange Exception, because you blindly increment i without even checking if that is a valid index.

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 NielsvSchooten · Oct 01, 2015 at 02:30 PM

first in your start function you declare the carrotArray and give it a size. Then you try to get elements from the array, while it is empty. The array needs to be filled before you even try to get something from it. try this:

 void Start () {
      carrotArray = new Plant[1];
      carrotArray.Add(new Plant());
      carrotArray[0].GetComponent<SpriteRenderer>().sprite = CS0;
  }
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 DiegoSLTS · Oct 01, 2015 at 02:03 PM

First make sure i is different than 0 at the begining, otherwhise this line:

 carrotArray = new Plant[i];

Will create an array of zero elements.

Anyway, the next line will always throw an error for 2 reasons. First, if the array has "i" elements, the valid indexes will go from zero to "i-1", carrotArray[i] is outside the array. Second, the previous line created an empty array of size "i", there are no elements in the array, so you'll try to get a component of a null reference.

 carrotArray[i].GetComponent<SpriteRenderer>().sprite = CS0;
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 lmstam · Oct 01, 2015 at 03:36 PM

I got it to work, thanks. I just gave the array a value of 100 because it won't go that far anyway. It's a temporary solution but I don't have to show the code (it's for a school project) and I'm fighting a deadline so I'll fix it properly later :)

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 rageingnonsense · Oct 01, 2015 at 09:04 PM 0
Share

You might want to consider using a List ins$$anonymous$$d, which abstracts away having to adjust the array size (and tends to be cleaner).

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

6 People are following this question.

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

Related Questions

Removing runtime array exception (C#) 1 Answer

How to make a 2D array of buttons? 2 Answers

How to play a random audio clip from an array in C#? 1 Answer

How to reference UI Panels, in an array for x and y? 0 Answers

Null reference exception in an if statement that checks for it. 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