Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

IndexOutOfRangeException: Index was outside the bounds of the array.

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by 479813005, Nov 2, 2018.

  1. 479813005

    479813005

    Joined:
    Mar 18, 2015
    Posts:
    39
  2. GiedriusUnity

    GiedriusUnity

    Unity Technologies

    Joined:
    Aug 23, 2016
    Posts:
    2
    For those stumbling onto this thread, I have since looked at the issue on our bug reporting platform:
    In C# and in most programming languages, arrays start at 0. I.e., an array of length = 1, would have an values at index = 0, an array of length = 2 would have values at index = 0 and index = 1, etc... Looking at the picture and values you pass above to the function variables, you start your for loop at the passed value of start(1), it tries to get the chars[1] value, when it only has a chars[0] value. When you call IntToString, make sure you're passing the correct value to start the loop on.
     
  3. 479813005

    479813005

    Joined:
    Mar 18, 2015
    Posts:
    39
    o_O incredible……Maybe I didn’t express it clearly.
    Will report an error when converting from the end of an array to a string。



    Or Test:
    Code (CSharp):
    1. public void Test(TextMeshProUGUI text)
    2.         {
    3.             int[] chararray = new int[] { 1, 2, 3, 4, 5 };
    4.             text.SetCharArray(chararray, 1, 1);
    5.         }
     
    Last edited: Nov 19, 2018
  4. 479813005

    479813005

    Joined:
    Mar 18, 2015
    Posts:
    39
  5. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,053
    Here is a revised IntToString() function.

    Code (csharp):
    1.  
    2. public static string IntToString(this int[] unicodes, int start, int length)
    3. {
    4.     if (start > unicodes.Length)
    5.         return string.Empty;
    6.  
    7.     int end = Mathf.Min(start + length, unicodes.Length);
    8.  
    9.     char[] chars = new char[end - start];
    10.  
    11.     int writeIndex = 0;
    12.  
    13.     for (int i = start; i < end; i++)
    14.     {
    15.         chars[writeIndex++] = (char)unicodes[i];
    16.     }
    17.  
    18.     return new string(chars);
    19. }
    20.  
    This change will be in the next release of the TMP package for Unity 2018.3 and up. I will also add additional bounds check in the SetCharArray() function.
     
    Last edited: Nov 19, 2018
    479813005 likes this.
  6. a7m3d_95

    a7m3d_95

    Joined:
    Dec 26, 2018
    Posts:
    1
    i have the same issue would be thankful for any help

    this is my error message :
    IndexOutOfRangeException: Index was outside the bounds of the array.
    mobileInputs.Update () (at Assets/scripts/mobileInputs.cs:57)


    and this is the part of the code in question:
    if (Input.touches.Length != 0)
    {
    if (Input.touches[0].phase == TouchPhase.Began)
    tap = true;
    isDragging = true;
    startTouch = Input.mousePosition;
    }
    // this is where the error is //
    else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
    {
    //startTouch = swipeDelta = Vector2.zero;
    isDragging = false;
    Reset();
    }
     
  7. attis911

    attis911

    Joined:
    Dec 24, 2018
    Posts:
    1
    I have the same problem IndexOutOfRangeException: Index was outside the bounds of the array. mobileInputs.Update
    Anyone can help me out what is the problem? -- else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
     
  8. TortoRacoon

    TortoRacoon

    Joined:
    Apr 17, 2019
    Posts:
    39
    I have the same issue, I was checking a tutorial, I wrote exactly the same but the guy in the video did not have any error. WHY!

    Code (CSharp):
    1.     public Transform origin;
    2.  
    3.     public float rayLength = 13.0f;
    4.  
    5.     private void Update()
    6.     {
    7.         if (Input.GetMouseButtonDown(0))
    8.         {
    9.             RaycastHit[] hits = null;
    10.  
    11.             hits = Physics.RaycastAll(origin.position, Vector3.forward, rayLength);
    12.  
    13.             if (hits.Length > 0)
    14.             {
    15.                 for (int i = 0; i < hits.Length; i++)
    16.                 {
    17.                     if ( hits[i].collider )
    18.                     {
    19.                         Debug.Log ("Hit: " + hits[1].collider.name );
    20.                     }
    21.  
    22.                 }
    23.             }
     
  9. minerandom

    minerandom

    Joined:
    May 12, 2019
    Posts:
    6
    When I run this project on unity it keeps doing this error:
    IndexOutOfRangeException: Index was outside the bounds of the array.
    FruitSpawner+<SpawnFruits>d__5.MoveNext () (at Assets/FruitSpawner.cs:26)
    and here is my code:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class FruitSpawner : MonoBehaviour {
    public GameObject fruitPrefab;
    public Transform[] spawnPoints;
    public float minDelay = .1f;
    public float maxDelay = 1f;
    // Use this for initialization
    void Start()
    {
    StartCoroutine(SpawnFruits());
    }
    IEnumerator SpawnFruits()
    {
    while (true)
    {
    float delay = Random.Range(minDelay, maxDelay);
    yield return new WaitForSeconds(delay);
    int spawnIndex = Random.Range(0, spawnPoints.Length);
    Transform spawnPoint = spawnPoints[spawnIndex];
    GameObject spawnedFruit = Instantiate(fruitPrefab, spawnPoint.position, spawnPoint.rotation);
    Destroy(spawnedFruit, 4f);
    }
    }
    }
    can someone please tell me how to fix this. Im using unity 2019
     
  10. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    622
    I'm having the same issue in Unity 2018.3.4f1 -

    Code (CSharp):
    1. string addStr = "";
    2.  
    3. foreach (string str in someListOfStr) // OP's err
    4. {
    5.     addStr += (str + "\n")
    6. }
    7.  
    8. // #####################################
    9.  
    10. IndexOutOfRangeException: Index was outside the bounds of the array.
    11. TMPro.TMP_Text.DrawUnderlineMesh (UnityEngine.Vector3 start, UnityEngine.Vector3 end, System.Int32& index, System.Single startScale, System.Single endScale, System.Single maxScale, System.Single sdfScale, UnityEngine.Color32 underlineColor) (at Library/PackageCache/com.unity.textmeshpro@1.3.0/Scripts/Runtime/TMP_Text.cs:5204)
    12. TMPro.TextMeshProUGUI.GenerateTextMesh () (at Library/PackageCache/com.unity.textmeshpro@1.3.0/Scripts/Runtime/TMPro_UGUI_Private.cs:3774)
    13. TMPro.TextMeshProUGUI.OnPreRenderCanvas () (at Library/PackageCache/com.unity.textmeshpro@1.3.0/Scripts/Runtime/TMPro_UGUI_Private.cs:1642)
    14. TMPro.TextMeshProUGUI.Rebuild (UnityEngine.UI.CanvasUpdate update) (at Library/PackageCache/com.unity.textmeshpro@1.3.0/Scripts/Runtime/TextMeshProUGUI.cs:209)
    15. UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/CanvasUpdateRegistry.cs:198)
    16. UnityEngine.Canvas:SendWillRenderCanvases() (at ?)
     
  11. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,053
    Can you provide an example of the text in "someListOfStr" so I can try to reproduce this?
     
  12. dausazam

    dausazam

    Joined:
    Jun 30, 2019
    Posts:
    1
    I tried to create sth like snake and ladder game. Trying to learn how to use Unity. I found the exact same error and it says that the problem is on line 87. The error doesn't show up while compiling but shows up when run the program. I use Unity 2019.1.8f1 Personal.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameController : MonoBehaviour
    6. {
    7.     public GameObject[] diceGameObject = new GameObject[6];
    8.     public GameObject[] tokenGameObject = new GameObject[4];
    9.     private int i;
    10.     private int dice;
    11.     private int move;
    12.     private int whiteNum;
    13.     private int blueNum;
    14.     private int redNum;
    15.     private int greenNum;
    16.     private int turn;
    17.     private int firstNum;
    18.     private int lastNum;
    19.     private bool gotSix = false;
    20.     private bool ladderCheck = false;
    21.     private bool snakeCheck = false;
    22.     private bool roolDone = false;
    23.     private int[,] ladder = {{3,20},{6,14},{11,28},{15,34},{17,74},{22,37},{38,59},{49,67},{57,76},{61,78},{73,86},{81,98},{88,91}};
    24.     private int[,] snake = {{8,4},{18,1},{26,10},{39,5},{51,6},{54,36},{56,1},{60,23},{75,28},{83,45},{85,59},{90,48},{92,25},{97,87},{99,63}};
    25.  
    26.     IEnumerator Roll(){
    27.         roolDone = true;
    28.         move = 0;
    29.         do{
    30.             for(i = 1; i<=10;i++){
    31.                 dice = Random.Range(0, 5);
    32.                 diceGameObject[dice].SetActive(true);
    33.                 yield return new WaitForSecondsRealtime((float)0.1);
    34.                 diceGameObject[dice].SetActive(false);
    35.                 yield return new WaitForSecondsRealtime((float)0.1);
    36.             }
    37.             dice = Random.Range(0, 5);
    38.             dice += 1;
    39.             diceGameObject[dice].SetActive(true);
    40.             move = move + dice + 1;
    41.             if(dice == 5){
    42.                 gotSix = true;
    43.                 yield return new WaitForSecondsRealtime(3);
    44.                 diceGameObject[dice].SetActive(false);
    45.             }else{
    46.                 gotSix = false;
    47.             }
    48.         }while(gotSix == true);
    49.         yield return new WaitForSecondsRealtime(3);
    50.         diceGameObject[dice].SetActive(false);
    51.         StartCoroutine(Move());
    52.     }
    53.    
    54.     IEnumerator Move(){
    55.         i = 0;
    56.         if(turn == 0){
    57.             while(i <= move){
    58.                 whiteNum += 1;
    59.                 i++;
    60.                 yield return new WaitForSecondsRealtime((float)0.5);
    61.             }
    62.         }else if(turn == 1){
    63.             while(i <= move){
    64.                 blueNum += 1;
    65.                 i++;
    66.                 yield return new WaitForSecondsRealtime((float)0.5);
    67.             }
    68.         }else if(turn == 2){
    69.             while(i <= move){
    70.                 redNum += 1;
    71.                 i++;
    72.                 yield return new WaitForSecondsRealtime((float)0.5);
    73.             }
    74.         }else if(turn == 31){
    75.             while(i <= move){
    76.                 greenNum += 1;
    77.                 i++;
    78.                 yield return new WaitForSecondsRealtime((float)0.5);
    79.             }
    80.         }
    81.         Check();
    82.     }
    83.  
    84.     public void Check(){
    85.         i = 1;
    86.         while(i <= ladder.Length || ladderCheck == false){
    87.             if(ladder[i, 0] == whiteNum){ //this line
    88.                 whiteNum = ladder[i, 1];
    89.                 ladderCheck = true;
    90.             }
    91.             if(ladder[i, 0] == blueNum){
    92.                 blueNum = ladder[i, 1];
    93.                 ladderCheck = true;
    94.             }
    95.             if(ladder[i, 0] == redNum){
    96.                 redNum = ladder[i, 1];
    97.                 ladderCheck = true;
    98.             }
    99.             if(ladder[i, 0] == greenNum){
    100.                 greenNum = ladder[i, 1];
    101.                 ladderCheck = true;
    102.             }
    103.             i++;
    104.         }
    105.         ladderCheck = false;
    106.         i = 0;
    107.         while(i <= snake.Length || snakeCheck == false){
    108.             if(snake[i, 0] == whiteNum){
    109.                 whiteNum = snake[i, 1];
    110.                 snakeCheck = true;
    111.             }
    112.             if(snake[i, 0] == blueNum){
    113.                 blueNum = snake[i, 1];
    114.                 snakeCheck = true;
    115.             }
    116.             if(snake[i, 0] == redNum){
    117.                 redNum = snake[i, 1];
    118.                 snakeCheck = true;
    119.             }
    120.             if(snake[i, 0] == greenNum){
    121.                 greenNum = snake[i, 1];
    122.                 snakeCheck = true;
    123.             }
    124.             i++;
    125.         }
    126.         snakeCheck = false;
    127.     }
    128.  
    129.     public void RollButtonOnClick(){
    130.         if(roolDone == false){
    131.             StartCoroutine(Roll());
    132.         }
    133.     }
    134.  
    135.     // Start is called before the first frame update
    136.     void Start()
    137.     {
    138.         print(snake);
    139.         whiteNum = blueNum = redNum = greenNum = 0;
    140.         turn = 0;
    141.         tokenGameObject[1].transform.position = new Vector3(-300,-200,0);
    142.     }
    143.  
    144.     // Update is called once per frame
    145.     void Update()
    146.     {
    147.         lastNum = (int)(whiteNum - Mathf.Floor(whiteNum / 10));
    148.         firstNum = (int)(Mathf.Floor(whiteNum / 10));
    149.         if((whiteNum <= 10 && whiteNum >= 1)|| (whiteNum >= 21 && whiteNum <= 30) || (whiteNum >= 41 && whiteNum <= 50) || (whiteNum >= 61 && whiteNum <= 70) || (whiteNum >= 81 && whiteNum <= 90)){
    150.             tokenGameObject[1].transform.position = new Vector3((-396 + (firstNum * 72)),(-44 + (firstNum * 72)),0);
    151.         }else if((whiteNum <= 20 && whiteNum >= 11)|| (whiteNum >= 31 && whiteNum <= 40) || (whiteNum >= 51 && whiteNum <= 69) || (whiteNum >= 71 && whiteNum <= 80) || (whiteNum >= 91 && whiteNum <= 100)){
    152.             tokenGameObject[1].transform.position = new Vector3((396 - (firstNum * 72)),(-44 + (firstNum * 72)),0);
    153.         }
    154.     }
    155. }
     
  13. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    622
    Sadly, I forgot where this happened. I tried to check out the stacktrace, but sadly, Unity's stacktraces can often be incredibly useless (such as this instance): It traced only to Unity files and didn't even trace back to my own file o_O otherwise I would've been happy to show the git diff (I used an arbitrary example).
     
  14. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    622
    Actually, I remember the source:

    upload_2019-7-1_11-2-0.png

    Code (CSharp):
    1. [
    2.     "test memo --5b23cb630d413804dae5945b",
    3.     "some other memo test --xbprev"
    4. ]
    75% sure it was that ^ but 25% could've been this:

    Code (CSharp):
    1. [
    2.     "test memo --kiroprev2",
    3.     "test memo 2 --kiroprev2",
    4.     "blah blah --",
    5.     "test test --kiroprev2",
    6.     "test test 2 & and sign = and equals sign! --kiroprev2",
    7.     "test 3  --Unknown (pid==5b23cb630d413804dae5945b)",
    8.     "test 4  --5b23cb630d413804dae5945b"
    9. ]