Advertisement
ZhongNi

Counter

Jan 14th, 2025
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. using System.Collections;
  2. using TMPro;
  3. using UnityEngine;
  4.  
  5. public class Counter : MonoBehaviour
  6. {
  7.     [SerializeField] private TextMeshProUGUI _textCounter;
  8.  
  9.     private float _currentValue;
  10.     private bool _isActive;
  11.     private IEnumerator _coroutine;
  12.     private WaitForSeconds _wait;
  13.  
  14.     private void Start()
  15.     {
  16.         _currentValue = 0f;
  17.         _wait = new WaitForSeconds(.5f);
  18.         _isActive = false;
  19.         _coroutine = IncreaseValue();
  20.     }
  21.  
  22.     public void Increase()
  23.     {
  24.         if (_isActive)
  25.         {
  26.             StartCoroutine(_coroutine);
  27.             _isActive = false;
  28.         }
  29.         else
  30.         {
  31.             StopCoroutine(_coroutine);
  32.             _isActive = true;
  33.         }
  34.     }
  35.  
  36.     private IEnumerator IncreaseValue()
  37.     {
  38.         while (true)
  39.         {
  40.             _currentValue++;
  41.             _textCounter.text = _currentValue.ToString("");
  42.  
  43.             yield return _wait;
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement