How to organize the display of resource extraction rate (per second) by workers in 2D Unity I have workers hired, each of them mines 1 coin per second when buying a bonus of 2. Employing workers is endless, here’s how to display how much they mine (all together ) per second (in the text in the UI). How to make the total amount of resources they extract per second displayed?
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Collections; public class Coin : MonoBehaviour { public Text scoreText; //Поле отображения количества монет private int score; //Переменная с монетами private int bonus = 1; //Это бонусы, и один это то сколько игрок будет получать за клик в начале игры! private int workersCount, workersBonus = 1; //Это рабочие их количество// workersBonus отвечает за сохранение бонуса рабочих void Start () { StartCoroutine(BonusPerSec()); } public void Update() { scoreText.text = score + "$"; } ///////Кнопка найма рабочего public void HireWorker(int index) { if (score >=shopCost [index]) { workersCount++; score -= shopCost[index]; } } IEnumerator BonusPerSec() { while (true) { score += (workersCount * workersBonus); //Добавление по 1 монетке каждую секунду yield return new WaitForSeconds(1); //В скобке это сколько длиться пауза } } IEnumerator bonusTimer(float time, int index) { shopBttns[index].interactable = false; //отключает возможность нажатия if(index == 0 && workersCount >0) //дает возможность использовать под разные бонусы workersCount >0) если их больше чем 0 { workersBonus *= 2; //бонус дает рабочим добывать не по 1 а по 2 yield return new WaitForSeconds(time); workersBonus /= 2; } shopBttns[index].interactable = true; } ////Активация таймера бонуса public void startBonusTimer(int index) //обознач индекс кнопки { int cost = 2 * workersCount; shopBttnsText[2].text = "КУПИТЬ УСКОРЕНИЕ ДЛЯ РАБОЧИХ:" + cost; if(score >= cost) { StartCoroutine(bonusTimer(bonusTime[index], index)); score -= cost; } }