Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Simple Clock (AM/PM, not Military)

Discussion in 'Scripting' started by Darknuke, Mar 8, 2011.

  1. Darknuke

    Darknuke

    Joined:
    Jan 31, 2011
    Posts:
    223
    I'm trying to make a simple clock. This should be easy right? I don't know why it won't work for me. Here's the code I have so far.

    Code (csharp):
    1.     void HandleClock()
    2.     {
    3.         guiTime = Time.time;
    4.  
    5.         if (minute >= 59)
    6.         {
    7.             hour += 1;
    8.         }
    9.         if(hour == 0)
    10.         {
    11.             hour = 12;
    12.         }
    13.         else if(hour >= 13)
    14.         {
    15.             hour = 1;
    16.         }
    17.         minute = (guiTime * 10) % 59;
    18.  
    19.         clock = string.Format("{0:00}:{1:00}", hour, minute);
    20.        
    21.         if (hour != 12)
    22.             doOnce = false;
    23.  
    24.         if (!doOnce)
    25.         {
    26.             if (hour == 12  ampm == "am")
    27.                 ampm = "pm";
    28.             else if (hour == 12  ampm == "pm")
    29.                 ampm = "am";
    30.             doOnce = true;
    31.         }
    32.     }
    33.  
    The minutes get to 59, but the hour won't change. If I change the if statement to >= 58, the hour goes up 6 times. I could do some kind of ghetto rig to get that to work, but that seems counter-productive and I feel like I should learn it the correct way.

    I googled how to do a clock and also looked up in the unity forums how to do it, but found nothing. All I find are countdown timer samples, which helped me at least get to this point. I hope someone can point me in the right direction here. I'm sure it's something simple, but I don't know what it is.

    Thanks!
     
  2. Dusho

    Dusho

    Joined:
    Jul 10, 2010
    Posts:
    22
  3. Darknuke

    Darknuke

    Joined:
    Jan 31, 2011
    Posts:
    223
    I thought there was some sort of command for time... I just couldn't find it anywhere. Thanks

    EDIT: So apparently DateTime doesn't work. I don't want it to be real life time. I'm trying to make an in game clock. Can DateTime still manage this and I'm just missing it?
     
    Last edited: Mar 8, 2011
  4. Darknuke

    Darknuke

    Joined:
    Jan 31, 2011
    Posts:
    223
    I figured out how to make a date using
    Code (csharp):
    1. guiTime = new DateTime(2012, 1, 1, 7, 0, 0);
    , but I can't seem to get it to add minutes or hours to it. I put guiTime.AddHours(5); and it does nothing. Same for minutes.

    EDIT: Figured it out. I was putting guiTime.AddMinutes(#); to add stuff. I had to do guiTime = guiTime.AddMinutes(#)... Woops
     
    Last edited: Mar 8, 2011
  5. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Hello,

    I don't see why you can't get it working with the Unity code. I'd imagine that your problem is that the 'minute' variable is getting reset to zero before the hour increments. If I were you, I'd just forget about setting the hours based on minutes, and set the hours based on guiTime as well. For example: (Written in browser - may not compile ;) )

    Code (csharp):
    1.  
    2. void HandleClock()
    3.     {
    4.         guiTime = Time.time; //set guiTime
    5.         second = Mathf.Round(Mathf.Repeat(guiTime, 60));  //use Mathf.Repeat to make the seconds reset to 0 when they get to 60
    6.         minute = Mathf.Floor(Mathf.Repeat(guiTime / 60, 60));  //one minute = 60 seconds - once again, reset minutes to 0 when they get to 60
    7.     hour = Mathf.Repeat(Mathf.Floor(guiTime / 3600), 13);  //this one's trickier - one hour = (60 * 60 seconds), and hours need to reset to 0 when they hit twelve
    8.  
    9.         if((Mathf.Floor(guiTime / 47000) % 2) == 0) {  //13 hours = 47000 seconds. guiTime / 47000 is even, then we need AM, otherwise, PM
    10.               ampm = "am";
    11.         } else {
    12.               ampm = "pm";
    13.         }
    14.  
    15.         clock = String.Format("{0:00}:{1:00}:{2:00} {3}", hour, minute, second, ampm);  //final clock string
    16. }
    17.  
    Besides being cleaner/easier to read (in my humble opinion), the hours, minutes, and seconds are independent of each other so you can kill one without killing the others.
    Hope this helps.

    -Lincoln Green