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 : 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.
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)
Your answer
Follow this Question
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