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. Dismiss Notice

4.6 UI: ArgumentOutOfRangeException: Argument is out of range. Parameter name: index

Discussion in 'Scripting' started by pKallv, Jan 2, 2015.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    I am following a tutorial doing a list view and is trying to add an input field to the canvas and get this message. Could someone please help me understand what i do wrong here, can't figure it out myself?

    Here is the scene:



    I get the error when I click on the input field.

    The code i use is:
    Code (csharp):
    1.  
    2. usingUnityEngine;
    3. usingSystem.Collections;
    4. usingSystem.Collections.Generic;
    5. usingUnityEngine.UI;
    6.  
    7.  
    8. public class setupScrollableList_Script : MonoBehaviour {
    9.  
    10.     public GameObject itemPrefab;
    11.     public int itemCount = 10;
    12.     public int columnCount = 1;
    13.     public Scrollbar theScrollBar;
    14.  
    15.    void Start () {
    16.  
    17.    RectTransform rowRectTransform = itemPrefab.GetComponent<RectTransform>();
    18.    RectTransform containerRectTransform = gameObject.GetComponent<RectTransform>();
    19.  
    20.    //calculate the width and height of each child item.
    21.    float width = containerRectTransform.rect.width / columnCount;
    22.    float ratio = width / rowRectTransform.rect.width;
    23.    float height = rowRectTransform.rect.height * ratio;
    24.    int rowCount = itemCount / columnCount;
    25.    if (itemCount % rowCount > 0)
    26.                   rowCount++;
    27.  
    28.       //adjust the height of the container so that it will just barely fit all its children
    29.       float scrollHeight = height * rowCount;
    30.       containerRectTransform.offsetMin = newVector2(containerRectTransform.offsetMin.x, -scrollHeight / 2);
    31.       containerRectTransform.offsetMax = newVector2(containerRectTransform.offsetMax.x, scrollHeight / 2);
    32.  
    33.       int j = 0;
    34.       for (int i = 0; i < itemCount; i++)
    35.        {
    36.       //this is used instead of a double for loop because itemCount may not fit perfectly into the rows/columns
    37.          if (i % columnCount == 0)
    38.          j++;
    39.  
    40.          //create a new item, name it, and set the parent
    41.          GameObject newItem = Instantiate(itemPrefab) asGameObject;
    42.          newItem.name = gameObject.name + " item at (" + i + "," + j + ")";
    43.           newItem.transform.SetParent(gameObject.transform, false);
    44.  
    45.          //move and size the new item
    46.          RectTransform rectTransform = newItem.GetComponent<RectTransform>();
    47.  
    48.          float x = -containerRectTransform.rect.width / 2 + width * (i % columnCount);
    49.          float y = containerRectTransform.rect.height / 2 - height * j;
    50.          rectTransform.offsetMin = newVector2(x, y);
    51.  
    52.          x = rectTransform.offsetMin.x + width;
    53.          y = rectTransform.offsetMin.y + height;
    54.          rectTransform.offsetMax = newVector2(x, y);
    55.       }
    56.  
    57.       theScrollBar.value = 1;
    58.  
    59.     }
    60.  
    61.  
    62. }
     
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Can you post the full error message with the line number etc....
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Thanks for answering. There is no line numbers to my code, this is what i have:
     
  4. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    I just tested without the code and get the same result??
     
  5. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Very strange, i deleted the first inputField and added a new one and now the errors are gone? ...maybe some type of bug.

    Problem seems to be solved and thanks for your response.