I have a tutorial program for children. The point is this: a random example is generated from the numbers a and b and a random sign is generated. Everything works fine, but I can't make the counter of correct answers (the prav variable). The script assigned to the buttons (otvet1, otvet2, otvet3) and example (zadacha), but I get some sort of nonsense. PLEASE HELP ALREADY HEAD IS ON 2 DAY THE SIT DOES NOT EXIT

using UnityEngine; using UnityEngine.UI; using System.Collections; public class GameControl : MonoBehaviour { public int a, znak, b, c, count, otvet, randomtext, tochnosykablyat; static int prav=0; public Text otvet1; public Text otvet2; public Text otvet3; public Text zadacha; public Text Count; void Start() { Yravnenie(); } void Yravnenie() { count++; //if(count<21) //{ Application.Quit(); Count.text = count+"/20"; } a = Random.Range(1, 21); b = Random.RandomRange(1, 21); znak = Random.RandomRange(1, 3); if (znak == 1) { zadacha.text = a + " + " + b + " = ?"; otvet = a + b; } if (znak == 2 && a >= b) { zadacha.text = a + " - " + b + " = ?"; otvet = a - b; } if (znak == 2 && a < b) { zadacha.text = a + " + " + b + " = ?"; otvet = a + b; } randomtext = Random.RandomRange(1, 4); tochnosykablyat = randomtext; if (randomtext == 1) otvet1.text = otvet.ToString(); if (randomtext == 2) otvet2.text = otvet.ToString(); if (randomtext == 3) otvet3.text = otvet.ToString(); a = Random.Range(2, 16); if (a == otvet) a--; if (randomtext == 1) otvet3.text = a.ToString(); if (randomtext == 2) otvet1.text = a.ToString(); if (randomtext == 3) otvet2.text = a.ToString(); b = Random.RandomRange(17, 31); if (b == otvet) b--; if (randomtext == 1) otvet2.text = b.ToString(); if (randomtext == 2) otvet3.text = b.ToString(); if (randomtext == 3) otvet1.text = b.ToString(); } void OnMouseUpAsButton() { if (gameObject.name == "otvet1" && tochnosykablyat == 1) { prav = prav + 1; Yravnenie(); } if (gameObject.name == "otvet2" && tochnosykablyat == 2) { prav = prav + 1; Yravnenie(); } if (gameObject.name == "otvet3" && tochnosykablyat == 3) { prav = prav + 1; Yravnenie(); } Count.text = prav.ToString(); } } 
  • And what result in fact is displayed in Count.text? - Alexander Danilovsky

2 answers 2

If I understood everything correctly, then you have three buttons. And three instances of the class "GameControl". At the same time, there is only one variable with the static modifier - the win count. The problem is that the variable "tochnosykablyat" is different in each instance. This means that after a visual update of the information in the buttons in other buttons, the value of the correct answer remains the same.

Try "tochnosykablyat" to also make a static variable

 public static int tochnosykablyat; 

Offtopical afterword. Please try to avoid such a strange naming of variables as "tochnosykablyat" ...

  • It will be especially ridiculous if the children of the quarrels are asked to look to learn ... If not these are older ... It will be inconvenient) - Andrew
  • Thank you so much for the help! everything is working! but about the name of the variable, I got very angry just, without stopping and eating, I sit and write, and there’s such a hat, although the application is almost ready - Mikhail Cheremisin
  • @ Mikhail Cheremisin, do not forget to close your questions after the decision. And vote for the answers. - Alexander Danilovsky

Create an empty object on the stage and hang this script:

 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Sample : MonoBehaviour { private int _a, _b; private string _text; private Znak CurrZnak; public int UserWinCounter; public int PCWinCounter; public int ExpectedRezult = 0; public int[] Otvets = new int[3]; // Use this for initialization void Awake () { NewEquation(); } public void NewEquation() { _a = Random.Range(7, 31); _b = Random.Range(4, _a); CurrZnak = (Znak) Random.Range(0, 2); switch (CurrZnak) { case (Znak.Plus): _text = _a + " + " + _b + " = ?"; ExpectedRezult = _a + _b; break; case (Znak.Minus): _text = _a + " - " + _b + " = ?"; ExpectedRezult = _a - _b; break; } Otvets[0] = ExpectedRezult; Otvets[1] = ExpectedRezult + Random.Range(1, 3); Otvets[2] = ExpectedRezult - Random.Range(1, 3); Shuffle(Otvets); } public enum Znak { Plus = 0, Minus = 1 } private void Shuffle<T>(T[] array) { int n = array.Length; while (n > 1) { n--; int k = Random.Range(0, n + 1); T value = array[k]; array[k] = array[n]; array[n] = value; } } void OnGUI() { GUI.Label(new Rect(10, 10, 100, 20), _text); if (GUI.Button(new Rect(10, 50, 50, 50), Otvets[0].ToString())) CheckWinnerWithNumber(Otvets[0]); if (GUI.Button(new Rect(70, 50, 50, 50), Otvets[1].ToString())) CheckWinnerWithNumber(Otvets[1]); if (GUI.Button(new Rect(130, 50, 50, 50), Otvets[2].ToString())) CheckWinnerWithNumber(Otvets[2]); GUI.Label(new Rect(10, 100, 400, 20), "PC Win:" + PCWinCounter + "; User Win: " + UserWinCounter); } private void CheckWinnerWithNumber(int otvet) { if (otvet == ExpectedRezult) { UserWinCounter++; NewEquation(); } else { PCWinCounter++; NewEquation(); } } } 

Do not forget to mark the most useful of answers as true.