Tell me, please, how to implement the functionality I need. There are some points that should be counted when executing the void OnTapSuccess function (the function is performed when the touch screen is clicked). Points must be counted from 0 to 10. From the variable where points will be recorded I will need to take them away (spend).

 int count = 0; //Тап по экрану произошел void OnTapSuccess(int fingerId, float heldTime) { count++; //количество нажатий на экран Debug.Log("Tapped Count: " + count + "\r\n" + "Finger ID: " + fingerId + "\r\n" + "Held Time: " + heldTime); scoreText.text = "Score: " + count.ToString(); GameObject.Find("Pride").GetComponent<Animator>().SetTrigger("Click"); } 

    2 answers 2

    If I understand you correctly, and you need to limit the recorded values ​​from 0 to 10, then convert your counter from the field to the property:

     int count; int Count { get { return count; } set { count = value < 0 ? 0 : value > 10 ? 10 : value; } } //Тап по экрану произошел void OnTapSuccess(int fingerId, float heldTime) { Count++; //количество нажатий на экран Debug.Log("Tapped Count: " + Count + "\r\n" + "Finger ID: " + fingerId + "\r\n" + "Held Time: " + heldTime); scoreText.text = "Score: " + Count.ToString(); GameObject.Find("Pride").GetComponent<Animator>().SetTrigger("Click"); } 
    • Yes, everything is correct, it was necessary to artificially limit the variable. Thank you, your decision came up. - Evgeniy

    Actually, here is the result in which the solution of comrade @Mirdin is used. I do not pretend to the brevity of the code)) all the same, this is the first thing I do))

      using UnityEngine; using UnityEngine.UI; using System.Collections; public class TouchCounter : MonoBehaviour { public GameObject[] obj; Text scoreText; int count = 0; int mana = 0; int Mana { get { return mana; } set { mana = value < 0 ? 0 : value > 10 ? 10 : value; } } void Start() { scoreText = GameObject.Find("Score").GetComponent<Text>(); } float[] fingerIdTimer = new float[5] { 0f, 0f, 0f, 0f, 0f }; //5 fingers max bool[] fingerIdValid = new bool[5] { true, true, true, true, true }; //One determine invalid, must be rest in TouchPhase.Ended const float timeOut = 0.5f; //Anything more than 0 and less than timeOut value is tap void Update() { //Loop over all finger touching the screen for (int i = 0; i < Input.touchCount; i++) { //Will only increment if finger is valid if (fingerIdValid[i]) { fingerIdTimer[i] += Time.deltaTime; } //If we reach the time out value and finger is still valid reset the finger id if (fingerIdTimer[i] > timeOut && fingerIdValid[i]) { fingerIdTimer[i] = 0f; //Reset Held Time fingerIdValid[i] = false; //Invalid OnTapFailed(i, fingerIdTimer[i]); } //After touch is released, Anything more than 0 and less than timerOut value is tap if (Input.GetTouch(i).phase == TouchPhase.Ended) { if (fingerIdTimer[i] > 0 && fingerIdTimer[i] < timeOut) { OnTapSuccess(i, fingerIdTimer[i]); } fingerIdTimer[i] = 0f; //Reset Held Time when released fingerIdValid[i] = true; //Reset Invalid when released } } switch (Mana) //ограничение до 10 нужно было тут { case 0: break; case 1: obj[1].GetComponent<SpriteRenderer>().color = new Color(255, 255, 255, 255); break; case 2: obj[2].GetComponent<SpriteRenderer>().color = new Color(255, 255, 255, 255); break; case 3: obj[3].GetComponent<SpriteRenderer>().color = new Color(255, 255, 255, 255); break; case 4: obj[4].GetComponent<SpriteRenderer>().color = new Color(255, 255, 255, 255); break; case 5: obj[5].GetComponent<SpriteRenderer>().color = new Color(255, 255, 255, 255); break; case 6: obj[6].GetComponent<SpriteRenderer>().color = new Color(255, 255, 255, 255); break; case 7: obj[7].GetComponent<SpriteRenderer>().color = new Color(255, 255, 255, 255); break; case 8: obj[8].GetComponent<SpriteRenderer>().color = new Color(255, 255, 255, 255); break; case 9: obj[9].GetComponent<SpriteRenderer>().color = new Color(255, 255, 255, 255); break; case 10: obj[10].GetComponent<SpriteRenderer>().color = new Color(255, 255, 255, 255); break; } } //Tap was successful void OnTapSuccess(int fingerId, float heldTime) { count++; //Increment the tap count mana++; //инкремент Маны до 10 Debug.Log("Tapped Count: " + count + "\r\n" + "Finger ID: " + fingerId + "\r\n" + "Held Time: " + heldTime); scoreText.text = "Score: " + count.ToString(); GameObject.Find("Pride").GetComponent<Animator>().SetTrigger("Click"); } //Tap failed (Time out Occured) void OnTapFailed(int fingerId, float heldTime) { Debug.Log("Tap Failed: " + fingerId); } } 
    • @Mirdin is what everything is for)) - Evgeniy