Update Unity AI Text every x minutes
I have a day of the year, a year and an hour of the day concatanated to form a UI text value in the top left of the page. I am forming the above with an index from a dayOfYear array, followed by a number for the year, followed by an index of a hourInDay array.
For example, this creates the following: '1st Janauary 2371 00:00'
I want to move the hour along by 1 every 5 minutes in real world time, then when it gets to 23:00, reset the hour array and increment the day array to show 2nd January and so on.
Displaying the above works fine, but when I try to run a function to incrementthe the hour every five minutes, nothing happens but there are no errors in the console. I have tried a co-routine, I have tried with invokerepeating on a function which increments the current array index for the hour and I have also tried on top of this, setting a reference to the UI text again in Update(); I also have a reference to UnityEngine.UI.
My code below. Can anyone tell me where I am going wrong? Like I say, when I start the game the correct date is shown based on the Array indexes concatenated, but nothing progresses after 5 seconds.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class PassageOfTime : MonoBehaviour
{
public float yearNo;
public string dayNo;
public int currentDay;
public string hourNo;
public int currentHour;
public string gameDateTime;
public Text DateTimeText;
string[] hourInDay = new string[]
{
"00:00",
"01:00",
"02:00",
"03:00",
"04:00",
"05:00",
"06:00",
"07:00",
"08:00",
"09:00",
"10:00",
"11:00",
"12:00",
"13:00",
"14:00",
"15:00",
"16:00",
"17:00",
"18:00",
"19:00",
"19:00",
"20:00",
"21:00",
"22:00",
"23:00"
};
string[] dayInYear = new string[]
{
"January 1st",
"January 2nd",
"January 3rd",
"January 4th",
"January 5th",
"January 6th",
"January 7th",
"January 8th",
"January 9th",
"January 10th",
"January 11th",
"January 12th",
"January 13th",
"January 14th",
"January 15th",
"January 16th",
"January 17th",
"January 18th",
"January 19th",
"January 20th",
"January 21st",
"January 22nd",
"January 23rd",
"January 24th",
"January 25th",
"January 26th",
"January 27th",
"January 28th",
"January 29th",
"January 30th",
"January 31st",
"February 1st",
"Feburary 2nd"
};
void incrementYear()
{
yearNo += 1;
}
void incrementDay()
{
currentDay += 1;
currentHour = 0;
}
void incrementHour()
{
currentHour += 1;
}
void ProgressTime()
{
incrementHour();
DateTimeText = GetComponent<UnityEngine.UI.Text>();
DateTimeText.text = gameDateTime;
}
// Use this for initialization
void Start()
{
DateTimeText = GetComponent<UnityEngine.UI.Text>();
currentDay = 0;
currentHour = 0;
dayNo = dayInYear[currentDay];
hourNo = hourInDay[currentHour];
yearNo = 2371;
gameDateTime = dayNo + " " + yearNo + " " + hourNo;
DateTimeText.text= gameDateTime.ToString();
InvokeRepeating("ProgressTime", 5.0f, 5.0f);
}
// Update is called once per frame
void Update()
{
DateTimeText.text = gameDateTime.ToString();
}
Answer by Vicarian · Jun 30, 2017 at 09:44 PM
So to clean this up a bit, utilize InvokeRepeating to add hours to a DateTime variable is sufficient. You do not need arrays to store all of your date/time information. You can simply calculate a time based on how many InvokeRepeating ticks have been handled. Using the System.DateTime class will make calculating time significantly easier.
If you have a year in which you want the game to start (2371), a time at which to start (January 1 at midnight), then you can initialize a DateTime variable and add hours to it on each InvokeRepeating tick.
using System;
using UnityEngine;
using UnityEngine.UI;
public class ExampleClass : MonoBehaviour {
// Initialize a new DateTime object to the first of January at midnight, 2371
DateTime gameTime = new DateTime(2371, 1, 1, 0, 0, 0);
void Start() {
// Begin repeatedly updating the time using InvokeRepeating
InvokeRepeating("IncrementTime", 5, 5);
UpdateTime();
}
void IncrementTime() {
// Access and update the gameTime DateTime variable by one hour
gameTime = gameTime.AddHours(1);
UpdateTime();
}
void UpdateTime() {
// Update the display of your time UI Component here
GetComponent<Text>().text = gameTime.ToString();
}
}
You might need to fiddle with it a bit to get it to your liking, but that should get you started. Attach the above to a script on the gameObject containing your Time text UI element. Or if you put it elsewhere, you'd need to change how it's referenced in the script, of course. Also refer to the MSDN for C# classes that will help you write more efficient code. The DateTime class will automatically increment the day, month and year when you simply use the AddHour function. Examples are generally provided as well.
Your answer
Follow this Question
Related Questions
why this code doesnt rewind the time ? 1 Answer
Handling inaccuracies between update loop and time in playmode tests. 0 Answers
How do GameObjects detect void Update and how to make my custom one. 0 Answers
list update in custom inspector 0 Answers
How to Get Speed to Very Gradually Pick Up Over Time? 2 Answers