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 DayyanSisson · Jul 04, 2012 at 02:27 AM · arraydebugintwhilearray-out-of-range-except

Array Is Out Of Range

I'm using a while loop to find the last open spot in a string array with 10 elements. Since it is an array, I can't just add a string to the end of it, so what I did was create the array with the max number of spots it can have, and whenever I needed to add a string to it, I would use this code to find the last open spot. I would then make that last open spot part of the array:

 void SearchLastSpot () {
 
  int i = 0;
  bool searchLength = true;
  Debug.Log(i);
 
  while(searchLength){
  if(userQuotes[i] == ""){
  searchLength = false;
  lastOpenSpot = i;
  }else{
  searchLength = true;
  }
  i++;
  }
  Debug.Log(lastOpenSpot);
  }

And later on in the code:

 if(GUI.Button(new Rect(Screen.width/2, Screen.height/1.18F, 100, 50), "Done")){
  SearchLastSpot();
  userQuotes[lastOpenSpot] = newQuotes;
  userTypes[lastOpenSpot] = newQuotesType;
  PlayerPrefsX.SetStringArray("User Quotes", userQuotes);
  PlayerPrefsX.SetStringArray("User Quote Type", userTypes);
  PlayerPrefs.SetInt("LastOpenSpot", lastOpenSpot);
 }

So thats how I'm using it. I'm getting an ArrayIndexIsOutOfRange error for the line:

 if(userQuotes[i] == ""){

Which makes no sense cause I did a debug and i = 0, and there's more than 1 element in my array. What am I doing wrong here?

Comment
Add comment · Show 7
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 torrente · Jul 04, 2012 at 02:39 AM 0
Share

$$anonymous$$ove the debug to the first line of your while loop. It might be overstepping the bounds after a few runs. See if it is giving a value of i that is beyond the limit there.

avatar image DayyanSisson · Jul 04, 2012 at 02:59 AM 0
Share

Nope, no matter what I make i, or what the size of the array is, it keeps returning the same number.

avatar image DayyanSisson · Jul 04, 2012 at 03:01 AM 0
Share

I even wrote another script a couple $$anonymous$$utes ago that uses this while loop:

 wp = GameObject.FindGameObjectsWithTag("InWaypoint");
  while(i < wp.Length){
  Debug.DrawLine(wp[i].transform.position, wp[a].transform.position);
  i++;
  a++;
  }

And it still gives me that same error. Its in a completely different script! And thats all thats in the other script.

avatar image whydoidoit · Jul 04, 2012 at 03:39 AM 1
Share

I don't see the definitions of these arrays and are you sure that a isn't exceeding the bounds in the second example. I'd highly recommend using List\<\> rather than arrays too.

Your first loop may well have a problem because you are checking for "" but the entries may be null - the normal way of handling that would be to do:

    if(string.IsNullOrEmpty(userQuotes[i]))
avatar image Kryptos · Jul 04, 2012 at 07:56 AM 0
Share

$$anonymous$$aybe reinventing the wheel is what you are doing wrong.

There are many collections (List, LinkedList, Dictionary...) that may fit better your particular use.

Show more comments

2 Replies

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

Answer by Wolfram · Jul 04, 2012 at 12:55 PM

Note that once your string array is already filled, searchLength will never be set to false before you run out of available slots in your array, thus causing your error. You need to make sure you never access elements with an index >= userQuotes.Length, and catch the case where you used up all available spots (because your array is full).

The Debug.Log(i); will of course always print "0", since you put this line directly after you initialized i with 0.

Concerning your other script posted in the comment, your "i" is not the problem, your "a" is. change the condition to

 while(i < wp.Length && a < wp.Length ){
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 DayyanSisson · Jul 04, 2012 at 04:22 PM 0
Share

Thanks, this solves the problem. I was confused because that script worked before with no problems, but then I combined two scripts together so that I didn't have to use GetComponent or use static variables and then it didn't work. But this solves the problem, thanks.

avatar image
-1

Answer by nventimiglia · Jul 04, 2012 at 12:45 PM

Just use a List.

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 DayyanSisson · Jul 04, 2012 at 04:11 PM 0
Share

Trust me, if I had the option to use a list, I would have.

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

9 People are following this question.

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

Related Questions

IndexOutOfRangeException: Array index is out of range. 1 Answer

Split each number in an int without converting to char and string first? 1 Answer

Why is this giving me an error? (ToBuiltin problems) 1 Answer

Finding the number of elements in a sub-array? 1 Answer

Index out of range inside IEnumerator 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