- Home /
Compare value in text with value in button
hi guys. i am trying to compare a value which is in Text with a value which is in Button but have no idea how to do it. i am trying for 6 days but no where find answer. float value1 = Random.Range(1, 10); float value2 = Random.Range(1, 12);
float add = value1 + value2;
no1.text = "" + value1;
no2.text = "" + value2;
ans.text = "" + add;
button4.GetComponentInChildren<Text>().text = "" + add;
how can i compare ans.text = add with button4.text = add.
Answer by BIPINGGDEV · May 20, 2019 at 12:44 PM
Add script to the button:
[SerializedField] Text tF;//textFieldWhoseValueNeedsToBeCompared
//Call this method when you want to do the comparison public void Compare(){
//I'm assuming button's name is the value you want to compare
if(GetComponent().name == tF.text){
Debug.Log("These are same");
} }
Answer by breban1 · May 20, 2019 at 03:19 PM
if(button4.GetComponentInChildren<Text>().text == ans.text)
// equal
else
// not equal
this is not working properly. this is work in all buttons not the right one
Your answer
![](https://koobas.hobune.stream/wayback/20220612201737im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUIText Problem With MENU 1 Answer
Change the text of a UI button on clicking it? 1 Answer
saving button text 1 Answer
How to lock UI elements position? 1 Answer
How to Change text of a Button or Input Field by clicking a button? 0 Answers