Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by nickproud · Jun 30, 2017 at 08:44 PM · unity 5updatetime

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();
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

MSDN DateTIme

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges