Hi
i’m trying to add every 2 seconds an value to a variable, but it adds the value every Frame how can i make it work.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class playerController : MonoBehaviour {
public Text money;
public Text playerMoney;
public bool test;
public static int actualPlayerMoney;
public static int actualMoney;
// Use this for initialization
void Start () {
test = true;
actualMoney = 0;
actualPlayerMoney = 0;
playerMoney.text = "0";
money.text = "Money: " + actualMoney;
}
// Update is called once per frame
void Update () {
actualMoney++;
SetText(money, "Money: ", actualMoney);
StartCoroutine(AddMoney());
SetText(playerMoney, "", actualPlayerMoney);
if (actualPlayerMoney == 50) test = false;
}
void SetText(Text text, string content, int count)
{
text.text = content + count.ToString();
}
IEnumerator AddMoney()
{
while (true)
{
yield return new WaitForSeconds(5);
Debug.Log("Sending");
yield return new WaitForSeconds(5);
}
}
}