As the title say i have followed a tutorial how to do this and all works well except that i am struggling how to identify and access the buttons i have created. I am able to find the individual buttons using "print (“Name: " + transform.GetChild(10));” so i know they are there. I can also see them hierarchy. The itemPrefab is the button.
The problem is how do i click on a button and by that are able to identify it and manipulate it such as trigger something.
I have one model of button that i use and create the table dynamically.
Here is the code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class setupScrollableList_Script : MonoBehaviour {
public GameObject itemPrefab;
public int itemCount = 10;
public int columnCount = 1;
public Scrollbar theScrollBar;
public InputField theInputField;
// Use this for initialization
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 = new Vector2(containerRectTransform.offsetMin.x, -scrollHeight / 2);
containerRectTransform.offsetMax = new Vector2(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) as GameObject;
newItem.name = "tempBut-" + i;
newItem.transform.SetParent(gameObject.transform, false);
//myList[i].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 = new Vector2(x, y);
x = rectTransform.offsetMin.x + width;
y = rectTransform.offsetMin.y + height;
rectTransform.offsetMax = new Vector2(x, y);
}
// Set the scroll bar so the first item is showed when starting
theScrollBar.value = 1;
GameObject xxxx = GameObject.Find ("tempBut-3");
//print (xxxx.name);
print (">>> END <<<");
}
This is probably a very simple problem but i am stuck and would really appreciate some help.