Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Array index out of range (when it shouldn’t)

Discussion in 'Scripting' started by Shushustorm, Aug 25, 2015.

  1. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Hello everyone!
    I get an array index out of range error when trying to run the following code:

    Code (CSharp):
    1. for (int i = UI_HighlightColors.Length; i > 0; i--) {
    2.     Debug.Log("SETTING COLOR TO: UI_HighlightColors[" + i.ToString() + "]" );
    3.     UI_Highlight[UI_ElementNumber].GetComponent<Image>().color = UI_HighlightColors[i];
    4.     yield return new WaitForSeconds(AnimationWaitTime);
    5. }
    But really don’t know why. Do you have an idea why?
    I tried
    Code (CSharp):
    1. UI_Highlight[UI_ElementNumber].GetComponent<Image>().color = UI_HighlightColors[i-1];
    instead of the original line and it works without errors then, but I will assign the wrong colors in that case.
    Debug.Log will then tell me (which only makes sense) that the first color assigned is the second last of the array. But I want the last one to be assigned first.

    Many thanks in advance for any ideas,
    Greetings,
    Shu
     
  2. adnan-e94

    adnan-e94

    Joined:
    Dec 17, 2012
    Posts:
    70
    i starts from the arr.Length value, which is by 1 out of range.

    Try this:

    Code (CSharp):
    1. for (int i = UI_HighlightColors.Length - 1; i >= 0; i--) {
    2.     Debug.Log("SETTING COLOR TO: UI_HighlightColors[" + i.ToString() + "]" );
    3.     UI_Highlight[UI_ElementNumber].GetComponent<Image>().color = UI_HighlightColors[i];
    4.     yield return new WaitForSeconds(AnimationWaitTime);
    5. }
     
    Shushustorm likes this.
  3. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Thanks for your quick reply! I just tried it and I have the same problem that I have when using
    Code (CSharp):
    1. UI_Highlight[UI_ElementNumber].GetComponent<Image>().color = UI_HighlightColors[i-1];
    .
    The whole thing will start on the second latest of the array and not the latest.


    EDIT:
    I just noticed: You are right. I have a different problem.
    It did in fact start with the latest, but it didn't go down all the way to the first.
    The code that is working:

    Code (CSharp):
    1. for (int i = UI_HighlightColors.Length-1; i >= 0; i--) {
    2.     Debug.Log("SETTING COLOR TO: UI_HighlightColors[" + i.ToString() + "]" );
    3.     UI_Highlight[UI_ElementNumber].GetComponent<Image>().color = UI_HighlightColors[i];
    4.     yield return new WaitForSeconds(AnimationWaitTime);
    5. }
    I thought the -1 was the only change in your code, so I didn't change the > to >=.

    Thanks again for your help!
     
    Last edited: Aug 25, 2015
  4. adnan-e94

    adnan-e94

    Joined:
    Dec 17, 2012
    Posts:
    70
    I editted my post a few moments ago, what code exactly did you use?
     
  5. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    I also edited my last post. It works now perfectly!
    This is what I tried first, which didn't work as intended, because the loop didn't go through the whole array:
    Code (CSharp):
    1. for (int i = UI_HighlightColors.Length-1; i > 0; i--) {
    2.     Debug.Log("SETTING COLOR TO: UI_HighlightColors[" + i.ToString() + "]" );
    3.     UI_Highlight[UI_ElementNumber].GetComponent<Image>().color = UI_HighlightColors[i];
    4.     yield return new WaitForSeconds(AnimationWaitTime);
    5. }