Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Help with Date and Time Script

Discussion in 'Scripting' started by SKGate, Aug 16, 2018.

  1. SKGate

    SKGate

    Joined:
    Nov 11, 2016
    Posts:
    19
    Hello, I have a Date and Time script that I made using Time.time for my Game Clock. Everything seems to be working just fine. The problem I'm having is setting a Start point for game's Date and Time when the game starts. For example, I would like to set a Date that Time.time would add to when the player starts the game, such as 8/15/2018 instead of it starting at 00/00/0000 and my Time Starting at 12:00 Instead of 00:00.

    Also, my Clock is set to a 12 hour clock. How can I make it go from 12 to 1 instead of 12 to 0?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GameClock : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     private Text timeText;
    10.     [SerializeField]
    11.     private Text dateText;
    12.  
    13.     private float gameRunTime;
    14.  
    15.     private string ampm;
    16.     private float minutes;
    17.     private float hours;
    18.     private int day;
    19.     private int month;
    20.     private int year;
    21.  
    22.     private float currentHours;
    23.     private int currentDay;
    24.     private int currentMonth;
    25.     private int currentYear;
    26.  
    27.     void Start()
    28.     {
    29.         ampm = "PM";    
    30.     }
    31.  
    32.     void Update()
    33.     {
    34.         gameRunTime = Time.time;
    35.  
    36.         if (Input.GetKey(KeyCode.A))
    37.         {
    38.             Time.timeScale = 2;
    39.         }
    40.         ConvertTime();
    41.  
    42.     }
    43.  
    44.     private void ConvertTime()
    45.     {
    46.         minutes = Mathf.Floor(gameRunTime % 60);
    47.         hours = Mathf.Floor(gameRunTime / 60 % 12);
    48.         day = Mathf.FloorToInt(gameRunTime) / 1440 % 31;
    49.         month = Mathf.FloorToInt(gameRunTime) / 44640 % 12;
    50.         year = Mathf.FloorToInt(gameRunTime) / 535680;
    51.  
    52.         Display();
    53.     }
    54.  
    55.     private void Display()
    56.     {
    57.         timeText.text = hours.ToString("00") + ":" + minutes.ToString("00") + " " + ampm;
    58.         dateText.text = month.ToString("00") + "/" + day.ToString("00") + "/" + year.ToString("00");
    59.     }
    60.  
    61.     public void Play()
    62.     {
    63.         Time.timeScale = 1;
    64.         Debug.Log("Play Fired");
    65.     }
    66.  
    67.     public void FastForward()
    68.     {
    69.         Time.timeScale = 100;
    70.         Debug.Log("FastForward Fired");
    71.     }
    72. }
    73.  
    Script is attached.
    Thank You for your time.
     

    Attached Files:

  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
  3. SKGate

    SKGate

    Joined:
    Nov 11, 2016
    Posts:
    19
    Hello Doug_B,
    Thank You for your response. I am new to coding and trying to figure all this out.

    Quick question while I look over the reference material you linked me to, will this work hand and hand with Time.time? or should I start over from scratch?

    Again, thank you for your time.
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    It is a standard C# library so, yes, it will work with the Unity Time class. For example, you could call AddSeconds handing in Time.time to track your absolute time value.

    That depends on how you want your game clock to function and what you learn from reading up on both the time classes (C# and Unity). Don't be afraid to start afresh if you gain a better understanding of how to achieve something and think it appropriate to do so. :)
     
  5. SKGate

    SKGate

    Joined:
    Nov 11, 2016
    Posts:
    19
    Hello Doug_B,
    I've been looking over the Reference Material. I'm trying to figure out how to put it all together. My overall goal for my Game Clock is that of "The Sims 4". But I'm afraid that may be a bit ambitious for my level of knowledge. I will keep looking over the Reference Material and keep trying the trial and error approach.

    Thanks Again for your time.
     
    Doug_B likes this.
  6. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Yeah, sounds like a good plan.

    Just remember to start simple (e.g. can you get the current date / time and display that), then build it up step by step (e.g. can you add 59 minutes and see the hours roll over, then can you start to add in-game time to it), etc.

    Most of all - have fun with it. :)