How can I display the number of times a gameobject has been clicked?
I have several instantiated clones of a prefab. Each clone is assigned a different name corresponding to what the user inputs in a text field. Each name is put into a List.
I am able to get the number of times a clone has been clicked (Count) and I am able to check whether it is in the list. Then I am able to display the Count for each object.
My problem:
If containsItem == true, and h++, how can I display the Count for ALL identical clones at the same time?
containsItem = data[0].Contains(EventSystem.current.currentSelectedGameObject.name);
h = data[0].Where(s => s != null && s.Contains(EventSystem.current.currentSelectedGameObject.name)).Count() + 1;
TMP_Text temp = EventSystem.current.currentSelectedGameObject.GetComponentInChildren<TextMeshProUGUI>();
temp.SetText(h.ToString());
Right now, the Count will update and display, but only on one clone at a time.
Answer by streeetwalker · Feb 26, 2020 at 09:44 PM
Another variable the totals the h's - like totalClicks += h; and then display that?
I got it. I know how I worded it was kinda confusing. I wanted to display the number each object has been clicked, and then attach that number to all clones. If there was a duplicate object, then I wanted all duplicates to display the same number of times they had been clicked. Like this: https://gyazo.com/1f162deee098e8a478d0406361afb593
[1]: /storage/temp/153288-1f162deee098e8a478d0406361afb593.png
I did
T$$anonymous$$P_Text temp;
GameObject[] go = GameObject.FindGameObjectsWithTag("ItemButton");
foreach (GameObject ty in go)
{
if (ty.name == EventSystem.current.currentSelectedGameObject.name)
{
GameObject ui;
ui = ty.transform.GetChild(0).gameObject;
temp = ui.GetComponentInChildren<Text$$anonymous$$eshProUGUI>();
temp.SetText(totalClicks.ToString());
}
}
Thanks streetwalker <3