Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Udata · Nov 20, 2014 at 01:21 AM · uiprefabbuttontextchildren

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Display text above prefabs in Unity 4.6 0 Answers

Passing through a GameObject/Function to a button's OnClick 1 Answer

How to understand if 4.6 UI button is highlighted? 3 Answers

Measure text size for UI Text 1 Answer

Photon Instantiate Prefab Script Problems 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges