Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by scary_code_monkey · Aug 22, 2016 at 01:51 PM · uiinstantiateprefabscomponentreference

New UI - Referencing deep nested components on an prefab instantiation

I have created a visualy attractive dynamic "High score Board" (C#) using the new Unity UI system. : But I cannot find a way to populate each "rows" text felds (3 Days of searching) on what I would guess is a fundamental newie question :

How do you change the text on a nested Text component that is runtime instantiated from a prefab ?

I have tried all the direct methods - and learnt that you cannot create a reference in the root of the prefab that survives instantiation (but - hell - I could have got half of those wrong !) Everything works perfectly untill I want to populate the score data ! (yes - the data is there !)

Hierarchy : alt text Code : (On the controller GO)

 public class SummaryController : MonoBehaviour {
 
 public GameObject PanelHolder;
 private List<GameObject> listOfPanelItems;
 //---------------------------------------
     private void Show_HasResult_Panel(int ActivityCount){    
             
         var nextNameNumber = 0;
         for(var x = 0; x < ActivityCount; x++) {
             GameObject SummaryItemPanel = Instantiate (SummaryItemPanelPrefab) as GameObject;
             SummaryItemPanel.transform.SetParent(PanelHolder.transform);
             SummaryItemPanel.name = "SummaryItemPanel_"+nextNameNumber;             
             
                  // would like to have populated here  (directly on SummaryItemPanel)
                  // But : hey ho - cant get it to work, so added to list for later use
                  listOfPanelItems.Add(SummaryItemPanel);
                  nextNameNumber++;
         }
         for (var y = 0; y < ActivityCount; y++) {
              GameObject thisPanelItem = listOfPanelItems[y];
          GameObject B1_P1_Text_Item = thisPanelItem.transform.Find("B1_P1_Text").gameObject;
          Text B1_P1_Text_Val = B1_P1_Text_Item.GetComponent<Text> () as Text;
          B1_P1_Text_Val.text = "Howdy";
                }
 }

addendum: listOfPanelItems = new List(); is in Start() and all namespaces correctly included!

It seems so simple (and I would have no problem in a host of other languages) - I have RTFM (*over & over) - watched GB's of video and I still cant nail it ! : can someone put me out of my misery ! (and show me how I am being blind ?) Thanks.

ref-compnts.jpg (38.2 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by scary_code_monkey · Aug 30, 2016 at 08:12 PM

Ok – I am going to answer my own question : or at least post up the solution I found (Précis)

 //Hard set the prefabs internal ‘paths’
 private string[] myComponentPaths = {        
 "HozPanel/C1_Panel/B1_Panel/B1_P1_Text",
 "HozPanel/C1_Panel/B1_Panel/B1_P2_Text",
 "HozPanel/C2_Panel/B2_Panel/B2_P1_Text",
 "HozPanel/C2_Panel/B2_Panel/B2_P2_Text",
 "HozPanel/C3_Panel/B3_Panel/B3_P1_Image",
 "HozPanel/C3_Panel/B3_Panel/B3_P2_Text",
 };
 
 private string itterantName;
 private string itterantPath; 
 var nextNameNumber = 0;
 // Main loop for data “rows”
  
 for(var x = 0; x < ActivityCount; x++) { 
  GameObject SummaryItemPanel = Instantiate (SummaryItemPanelPrefab) as GameObject;
  SummaryItemPanel.transform.SetParent(PanelHolder.transform);
  itterantName = "SummaryItemPanel_"+nextNameNumber;
  SummaryItemPanel.name = itterantName;
 
 // inner loop for nested prefab components
  for(var z=0;z<myComponentPaths.Length;z++){
   // build dynamic 'path'
   itterantPath = (itterantName+"/"+ myComponentPaths[z]).ToString ();
   switch (z) {
    case 0:
     GameObject.Find (itterantPath).GetComponent<Text> ().text = row_B1_P1_Text;
    break; 
    << snip >>
    case 4:
     GameObject.Find (itterantPath).GetComponent<Image>().sprite =  Resources.Load<Sprite>(row_B3_P1_Image.ToString());
    break;
  }
 nextNameNumber++;
 }

All well and good - It works :

But - this can hardly be the best way - can it ??

Anyway I hope this helps someone - I wasted a hell of a lot of time trying figure this out for myself : the crucial bit was using the newly assigned name NOT trying to use the instantiation return GameObject (Which I still feel I should be able to use instead)

Comment
Add comment · Share
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

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

96 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The prefabs stop instantiating after some time. 1 Answer

Instantiating prefabs problems. Check the description 0 Answers

Attach prefabs to a video frame not seeker during video play or pause mode. 0 Answers

Screen Space Overlay canvas prefab becomes unclickable 1 Answer

How to get instantiate object(clone) to get the reference of the original object with scripts? 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