Get a text value using button click

I’m developing an UI system for my project. The UI will contain a list of UI ‘Panels’ where inside the panels there will be some texts and a button. I’ve created the list of the panels by Instantiation ‘Panel Gameobject’ . My problem is inside the panels I want to get the “Game ID” text value individually by clicking the button inside each panel. I’m nearly not getting any solution regarding this problem.

It would be very helpful if anyone get me out from this nightmare . Here is a picture I’ve


I’m not the best at using the most efficient coding practices, so others may point out if anything is wrong, but for the most part this is how i do it.

(you will need to drag the text object and button into the script in the inspector.)

using UnityEngine.UI;

public Text id1;
public GameObject button1;
public String textValue;

//Can then either set the value of the text in script or the inspector.
//Use this if you want to set it inside the script Inside a function like //Update()
id1.text = "Whatever";

//Button Press
public void GetTextValue()
//Depends on what you want to do here with the actual value.
//For this example i will put the value into a string.
    textValue = id1.ToString();

Hope this is slightly helpful.

@Amphelion for example, if we assume that we have a panel with 100 text UI elements in it, we could put all the text elements in an array by getting all the text components within the panel gameobject. Then we could get their values by writing:

    public GameObject ParentOfAllTexts;

    private void Start()

    void GetAndReturnValuesInAnArray()
        Text[] texts = ParentOfAllTexts.GetComponentsInChildren<Text>();

        foreach (Text text in texts)

You could replace this with a button and get a certain component or value within those buttons quite simply as well.