- Home /
Best method for updating children of instantiated buttons?
I am currently making a prototype using almost exclusively 4.6 UI objects (canvases, panels, buttons, etc.). I am very inexperienced with both Unity and C# so please do explain in as basic a manner as possible, and feel free to point out any mistakes I've made.
I am trying to make a list within a panel. I have created the panel and some tabs (buttons) on top which are all unchanging and fine for now. However the list itself is composed of items, and each item needs to be a button so that it can be pressed. These item/buttons in the list need to be created dynamically as each item in the list can be added or removed during runtime. So I created a prefab button, and the prefab contains a few children (strings and images for each column in the list, such as name, status, etc). I've played around with instantiate and am able to create buttons from that prefab now.
The problem is I can't find a good way to update the children/information in each button after it is created. I have tried a plethora of approaches and been tearing my hair out over it. The main options as I see it:
A. Place the script in the button prefab. I can then use a function like:
public Text itemName;
public void ChangeItemName () {
itemName.text = "test";
}
I then drag the text object in the hierarchy to that reference and put the function on a test button. This updates the name of the prefab only for some reason (I would expect it to affect instantiations of the prefab too since they also contain the script). But even if that did work I would need some way to reference each button individually anyway so that I can update specific ones when needed. I'm not sure how I would do that since they are created dynamically and I can't reference them beforehand in the hierarchy. Importantly I can't seem to get the prefab's scripts to keep working for the instantiated objects though.
B. Place the script in a "manager" game object that just contains scripts. From this position it becomes an issue of accessing the specific Text object (for example) in one of the dynamically created button somewhere else in the hierarchy. Within the function in which I instantiate a new item button, I have tried using:
testName = transform.Find("itemNameText");
// where "itemNameText" is the name of the
// Text object child of the button prefab in the hierarchy
// so instantiations also contain this child with the same name
testName.text = "test";
The problem here is transform.Find returns a transform instead of a Text object of course, and I can't seem to change the text characteristic of it. Even if I could, would it access the child of the instantiated button properly? Where do I even point out which object transform.Find should be searching through (since it only checks the children of one object, correct?). The documentation on transform.Find is very brief and seems to assume more knowledge than I have, and I can't find any good tutorials on it. There are also children in each button I would want to update frequently so I don't know if using any "find" method is a good idea, although some fields like the name wouldn't have to be changed more than the once when they are setup.
So what is the best approach for this? How can I get one of the above working, or would some other approach altogether be better? Thank you very much!