Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Having a timer problem using Time.time

Discussion in 'Scripting' started by ecnalyr, Jun 11, 2010.

  1. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    I am trying to make a sort of timer so that whenever an event happens, I take that instance of time and add 3 seconds to it.

    Code (csharp):
    1.  
    2. Script.MULTITIMER=Time.time+3;
    3.  
    The idea is that in the following "Script" I have a sort of timer that if it ever equals the previous time plus 3 seconds, the "Script" does special timer stuff.

    Code (csharp):
    1.  
    2. //Script
    3. static var MULTITIMER : int;
    4.  
    5. function Update ()
    6. {
    7.     if (Time.time==MULTITIMER)
    8.     //do special timer stuff here  
    9. }
    10.  
    Why is this not functioning as I intend?

    My understanding is that Time.time is simply the amount of time that has passed since the start of the scene or game (not sure which, doesn't matter here) in an integer. Since I want 3 seconds to pass before my if statement takes action, I start with Time.time+3, then wait for the game's new Time.time to = my initial Time.time+3.

    Any help would be appreciated.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1.     if (Time.time==MULTITIMER)
    That will pretty much never happen. If your timer is 3, Time.time is likely to be something like 3.017395836. Use >= instead of ==.

    --Eric
     
  3. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    Thank you very much, learned something new.