Search Unity

  1. Unity 2019.1 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:
    2,758
    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. eriuop

    eriuop

    Joined:
    Apr 17, 2019
    Posts:
    4
    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:
    1
    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. Xblade-Imperium42

    Xblade-Imperium42

    Joined:
    Jan 12, 2016
    Posts:
    593
    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:
    2,758
    Can you provide an example of the text in "someListOfStr" so I can try to reproduce this?