Question by
justin123089 · Apr 01, 2021 at 08:09 PM ·
uibuttonprogramming
Updating Button Text
Hello everyone I have a problem with my code. The button's text updates for two out of three of them, but when I click the one that is the right answer it never updates to the new answer.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Calculator : MonoBehaviour { public int requestedfinal, RandomAnswer1, RandomAnswer2; public string[] Answers; public int firstvalue, secondvalue, timevalue; public Text[] Guesses; public Text first, Operator, second, answer; string[] Operations = {"Add", "Subtract", "Multiplication", "Division" }; string chosenOperation; // Start is called before the first frame update void Start() {
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
ACalculator("Add");
}
if (Input.GetKeyDown(KeyCode.D))
{
ACalculator("Subtract");
}
if (Input.GetKeyDown(KeyCode.F))
{
ACalculator("Multiplication");
}
if (Input.GetKeyDown(KeyCode.G))
{
ACalculator("Division");
}
Answers[0] = "" + requestedfinal;
Guesses[0].text = Answers[0];
Answers[1] = "" + RandomAnswer2;
Guesses[1].text = Answers[1];
Answers[2] = "" + RandomAnswer1;
Guesses[2].text = Answers[2];
}
public void ACalculator(string operation)
{
secondvalue = Random.Range(1, 10);
firstvalue = Random.Range(1, 10);
if (firstvalue- secondvalue < 0)
{
timevalue = secondvalue;
secondvalue = firstvalue;
firstvalue = timevalue;
}
if(operation == "Add")
{
requestedfinal = firstvalue + secondvalue;
Operator.text = "+";
}
if (operation == "Subtract")
{
requestedfinal = firstvalue - secondvalue;
Operator.text = "-";
}
if (operation == "Multiplication")
{
requestedfinal = firstvalue * secondvalue;
Operator.text = "*";
}
if (operation == "Division")
{
requestedfinal = firstvalue / secondvalue;
Operator.text = "/";
}
/*
for (int i = 0; i < Guesses.Length; i++)
{
i = Random.Range(0, 2);
Answers[Random.Range(0, 2)] = Guesses[i].text;
}
Answers[0] = " " + RandomAnswer1;
Answers[1] = " " + RandomAnswer2;
Answers[2] = " " + requestedfinal;
Guesses[0].text = " " + Answers[0];
Guesses[1].text = " " + Answers[1];
Guesses[2].text = " " + Answers[2];
*/
first.text = "" + firstvalue;
second.text = "" + secondvalue;
RandomAnswer1 = requestedfinal + Random.Range(1, 3);
RandomAnswer2 = requestedfinal + Random.Range(-1, -3);
Answers[0] ="" + requestedfinal;
Guesses[0].text = Answers[0];
Answers[1] = "" + RandomAnswer2;
Guesses[1].text = Answers[1];
Answers[2] = "" + RandomAnswer1;
Guesses[2].text = Answers[2];
Debug.Log(RandomAnswer1 +" "+ RandomAnswer2 );
Debug.Log("1Value: " + firstvalue + " 2Value: " + secondvalue + " Requested Value: " + requestedfinal);
}
public void Guess1()
{
answer.text = Guesses[0].text;
if (answer.text == "" + requestedfinal)
{
StartCoroutine("Right");
}
}
public void Guess2()
{
answer.text = Guesses[1].text;
if (answer.text == "" + requestedfinal)
{
StartCoroutine("Right");
}
}
public void Guess3()
{
answer.text = Guesses[2].text;
if (answer.text == "" + requestedfinal)
{
StartCoroutine("Right");
}
}
IEnumerator Right()
{
yield return new WaitForSeconds(1);
int i = Random.Range(0, 3);
int chosen = Random.Range(0, Operations.Length);
ACalculator("" + chosen);
if (chosen == 0)
{
Operator.text = "+";
}
if (chosen == 1)
{
Operator.text = "-";
}
if (chosen == 2)
{
Operator.text = "*";
}
if (chosen == 3)
{
Operator.text = "/";
}
answer.text = "?";
}
}
capture.png
(8.1 kB)
capture1.png
(9.7 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613031632im_/https://answers.unity.com/themes/thub/images/avi.jpg)