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:
usingUnityEngine;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingUnityEngine.UI;
public class setupScrollableList_Script : MonoBehaviour {
public GameObject itemPrefab;
public int itemCount = 10;
public int columnCount = 1;
public Scrollbar theScrollBar;
void Start () {
RectTransform rowRectTransform = itemPrefab.GetComponent<RectTransform>();
RectTransform containerRectTransform = gameObject.GetComponent<RectTransform>();
//calculate the width and height of each child item.
float width = containerRectTransform.rect.width / columnCount;
float ratio = width / rowRectTransform.rect.width;
float height = rowRectTransform.rect.height * ratio;
int rowCount = itemCount / columnCount;
if (itemCount % rowCount > 0)
rowCount++;
//adjust the height of the container so that it will just barely fit all its children
float scrollHeight = height * rowCount;
containerRectTransform.offsetMin = newVector2(containerRectTransform.offsetMin.x, -scrollHeight / 2);
containerRectTransform.offsetMax = newVector2(containerRectTransform.offsetMax.x, scrollHeight / 2);
int j = 0;
for (int i = 0; i < itemCount; i++)
{
//this is used instead of a double for loop because itemCount may not fit perfectly into the rows/columns
if (i % columnCount == 0)
j++;
//create a new item, name it, and set the parent
GameObject newItem = Instantiate(itemPrefab) asGameObject;
newItem.name = gameObject.name + " item at (" + i + "," + j + ")";
newItem.transform.SetParent(gameObject.transform, false);
//move and size the new item
RectTransform rectTransform = newItem.GetComponent<RectTransform>();
float x = -containerRectTransform.rect.width / 2 + width * (i % columnCount);
float y = containerRectTransform.rect.height / 2 - height * j;
rectTransform.offsetMin = newVector2(x, y);
x = rectTransform.offsetMin.x + width;
y = rectTransform.offsetMin.y + height;
rectTransform.offsetMax = newVector2(x, y);
}
theScrollBar.value = 1;
}
}
