Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using TMPro;
- using UnityEngine;
- public class Counter : MonoBehaviour
- {
- [SerializeField] private TextMeshProUGUI _textCounter;
- private float _currentValue;
- private bool _isActive;
- private IEnumerator _coroutine;
- private WaitForSeconds _wait;
- private void Start()
- {
- _currentValue = 0f;
- _wait = new WaitForSeconds(.5f);
- _isActive = false;
- _coroutine = IncreaseValue();
- }
- public void Increase()
- {
- if (_isActive)
- {
- StartCoroutine(_coroutine);
- _isActive = false;
- }
- else
- {
- StopCoroutine(_coroutine);
- _isActive = true;
- }
- }
- private IEnumerator IncreaseValue()
- {
- while (true)
- {
- _currentValue++;
- _textCounter.text = _currentValue.ToString("");
- yield return _wait;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement