Increase a value by one every minute
I'm trying to increase a value by one every 60 seconds. This is what I got so far: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class DayCounter : MonoBehaviour {
public Text secondsText;
public Text dayText;
int day;
float seconds;
void Start ()
{
seconds = PlayerPrefs.GetFloat("Seconds Text", 0);
secondsText.text = "Seconds: " + seconds.ToString("00");
day = PlayerPrefs.GetInt("Day Text", 0);
dayText.text = "Day: " + day;
}
void Update ()
{
seconds = (int)(Time.time % 60f);
secondsText.text = "Seconds: " + seconds.ToString("00");
if (seconds >= 5)
{
day++;
seconds = 0;
secondsText.text = "Seconds: " + seconds.ToString("00");
dayText.text = "Day: " + day;
Debug.Log("a day has passed");
}
}
the 5 seconds mark and the seconds display are just for testing
The text display works fine. The problem is that after 5 seconds, the seconds counter gets stuck on zero, and the days counter keeps increasing forever.
I want a script that doesn't show the seconds display ingame, and increases the value of the Day variable by one every minute.
Any help would be appreciated ^^
Answer by Lineweaver · Mar 22, 2019 at 06:31 PM
I figured out how to do it using this post: https://stackoverflow.com/questions/45653990/increase-variable-x-amount-every-x-seconds
The final script looks like this:
public class DayCounter : MonoBehaviour {
public Text secondsText;
public Text dayText;
int day;
public int dayMark;
float seconds;
void Start ()
{
seconds = PlayerPrefs.GetFloat("Seconds Text", 0);
secondsText.text = "Seconds: " + seconds.ToString("00");
day = PlayerPrefs.GetInt("Day Text", 0);
dayText.text = "Day: " + day;
}
void Update ()
{
seconds += Time.deltaTime;
secondsText.text = "Seconds: " + seconds.ToString("00");
if(seconds >= dayMark)
{
DayCount();
dayText.text = "Day: " + day;
seconds = 0;
}
}
void DayCount()
{
day++;
}
}
Answer by Cornelis-de-Jager · Mar 22, 2019 at 04:06 AM
Use Coroutines:
public int day;
void Start () {
day = 0;
StartCoroutine(Timer(60));
}
IEnumerator Timer(int time) {
yield return WaitForSeconds(time);
day++;
seconds = 0;
secondsText.text = "Seconds: " + seconds.ToString("00");
dayText.text = "Day: " + day;
Debug.Log("a day has passed");
}
I used part of your answer, now my script looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DayCounter : $$anonymous$$onoBehaviour {
public Text secondsText;
public Text dayText;
int day;
float seconds;
void Start ()
{
seconds = PlayerPrefs.GetFloat("Seconds Text", 0);
secondsText.text = "Seconds: " + seconds.ToString("00");
day = PlayerPrefs.GetInt("Day Text", 0);
dayText.text = "Day: " + day;
StartCoroutine(Timer(5)); //5 seconds for testing
}
void Update ()
{
seconds = (int)(Time.time % 60f);
secondsText.text = "Seconds: " + seconds.ToString("00");
}
IEnumerator Timer(int time)
{
yield return new WaitForSeconds(time);
day++;
seconds = 0;
secondsText.text = "Seconds: " + seconds.ToString("00");
dayText.text = "Day: " + day;
Debug.Log("a day has passed");
}
}
After 5 seconds the day counter changes to one, but after that the counter doens't increase to 2. I tried activating the coroutine from the Update function, but then the day counter keeps increasing forever again. How can I make it so the counter changes every $$anonymous$$ute throughout the entire game?
Your answer
Follow this Question
Related Questions
the following Code after "yield return new WaitForSeconds();" not executed 1 Answer
Trigger a Timer 2 Answers
Farming game Time management 0 Answers
change timer float while still counting down 2 Answers
Milliseconds Timer Question 1 Answer