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 /
avatar image
0
Question by Xenon42 · Oct 14, 2016 at 10:58 AM · timedate

Using real-world date and time for in-game time mechanics

I'm trying to make a game similar to the recent XCOM remakes, and I want to recreate the time mechanics in those games' overworld maps. If you haven't played those games, basically I want to:

  • Get the real-world time at the moment the player starts a new game.

  • Automatically advance that starting time forwards by a certain amount at the start of the game (for example, moving the time 10 years ahead of the game's start).

  • Have that time advance at the same rate as real-time (1 in-game second to 1 real second), while also having the option of fast-forwarding or freezing the time at need.

  • Have the game trigger functions based on certain criteria (at the end of each month, for example).

Basically, I want to store the real-world date and time as a variable at the start of the game, then spend the rest of the game altering and checking that variable arbitrarily. How could I go about doing this?

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 tanoshimi · Oct 14, 2016 at 12:09 PM

System.DateTime.Now gives you the real-world time on the user's computer. Everything else you specify can be derived from that.

Comment
Add comment · Show 7 · 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
avatar image Xenon42 · Oct 14, 2016 at 11:06 PM 0
Share

How would I store that value, though? Is there a specific variable for date/time?

avatar image tanoshimi Xenon42 · Oct 15, 2016 at 06:05 AM 0
Share

Yes, DateTime. The first line of the description on that page states: "The Now property returns a DateTime value". And the first line of the example code shows a DateTime variable used to store that value:

 DateTime localDate = DateTime.Now;
avatar image Xenon42 tanoshimi · Oct 17, 2016 at 11:01 PM 0
Share

Ah, my apologies. I should have looked more carefully.

One further, slightly off-topic problem: I've made a system that allows me to speed up, slow down, and freeze the "global" time, and I'm currently trying to make all other in-game timers sync to it. I'm trying to make all timers store a reference to the WorldTimer script, so they can efficiently check the timeFactor variable every Update. But for some reason the code refuses to recognise the component variable. $$anonymous$$y script is below:

 public int timeFactor;
 public Component worldTimer;

 void Start () {

     worldTimer = gameObject.GetComponent<WorldTimer> ();
     timeFactor = worldTimer.timeFactor;
 }

The problem is with the timeFactor = worldTimer.timeFactor line. The code doesn't seem to recognise what worldTimer is referring to, and so refuses to acknowledge the timeFactor variable within the WorldTimer script. How can I fix this?

Show more comments
avatar image Bonfire-Boy · Oct 18, 2016 at 07:02 AM 0
Share

A couple of things that you might want to look out for, the importance of which will depend on the what exactly you're doing with the time in your game (and maybe also on the platform)....

1) People may be able to set the system clock. If this could matter in your game, you might want to connect to a server periodically to get the real time.

2) System.Time.Now is the local time. If this could matter in your game (eg if you don't want the duration of an effect to depend on whether or not they've changed time zones), you might want to use System.Time.UtcNow ins$$anonymous$$d (which gives you Greenwich $$anonymous$$ean Time).

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

59 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

Related Questions

Get System date 1 Answer

Cleaning this code up. 2 Answers

Count time when the game is not running (mobile device)? 2 Answers

How can I get the time since the epoch date in Unity3D? 2 Answers

putting the date into a variable 1 Answer


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