- Home /
Get a text value using button click
Hello, 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 attached:
Before I provide a solution, I want to make sure I understand your problem.
You want the GameID to change to whatever value that you have coded into the button? So, if when I hit button, and I want GameID to say "Level 1" it will say "Level 1" in that GameID?
Actually I tried to get the exact GameID text "ABC123" while I click the button next to it. I've taken these GameIDs from a list that I fetched from firebase database. Thanks for the help :) . I tried to solve this problem internally from the script but failed, later on I used drag n drop method which worked !
Same problem here, I have thousands of row of Game ID, nd Thousands of Play button. so how Can I bind each and everyone, also, these thousands are dynamic so it's changing time by time. So need something like pass GAME_ID as parameter.
Answer by Divinitize1 · Aug 20, 2019 at 04:17 PM
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.
Thanks for the solution. I actually tried to find the gameobject by script but unfortunately I was getting the value of last index of my Game_id list after clicking the panel buttons. Later on I tried similar method you provided and its working . Thank you very much for the help :) .
How can we get values from a gameobject array? What if I have like 100 different buttons with different values how can I get each own button value ?
I totally agreed. I have same problem, and have 1000s of rows from database, onclick go to 2nd page but with **GameID**
Answer by Llama_w_2Ls · Aug 18, 2020 at 04:47 PM
@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()
{
GetAndReturnValuesInAnArray();
}
void GetAndReturnValuesInAnArray()
{
Text[] texts = ParentOfAllTexts.GetComponentsInChildren<Text>();
foreach (Text text in texts)
{
Debug.Log(text.text);
}
}
You could replace this with a button and get a certain component or value within those buttons quite simply as well.