Search Unity

Problem in making a stopwatch

Discussion in 'Scripting' started by lucked, Apr 5, 2011.

  1. lucked

    lucked

    Joined:
    Jul 11, 2010
    Posts:
    111
    I am wanting to make a timer for my racing game that I'm making a help because I would not know how I'm doing if someone could ever help me or tell me some references so I can get the documentation. Thanks I hope you can help me.

    Note: I am using C#. Thanks.
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Use concept to your advantage.

    First you need some variables to maintain your start and stop times. Every OnGUI or Update use the time difference.

    A stop watch can be done by using an event, like an on trigger event to tell you the difference between the start time and the current time. Thus, giving you a stop watch.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class example : MonoBehaviour {
    6.     public float timeToStart=0.0;
    7.     public float timeToEnd=10.0;
    8.  
    9.     void OnGUI() {
    10.         float timeRemaining=timeToEnd - (Time.time - timeToStart);
    11.         GUI.Label(new Rect(10, 10, 100, 20), "Time left: " + timeRemaining);
    12.     }
    13. }
    14.  
     
  3. lucked

    lucked

    Joined:
    Jul 11, 2010
    Posts:
    111
    Thanks worked nicely thank you again.
     
  4. corey.stpierre

    corey.stpierre

    Joined:
    Nov 18, 2008
    Posts:
    79
    @lucked: I just released a plugin called Ultimate Timer that can the above and much more. It is a fully capable timer implementation that has stop, start, and reset functionality, not to mention a host of other timer related features like event handling, independent time scale, etc. You can check out the thread here:

    http://forum.unity3d.com/threads/83841

    @BigMisterB: You always beat me to the timer threads lol. I don't want you to think I'm hi-jacking your answers. I used to use the same kind of implementation, but came to the conclusion after a few projects that there just isn't enough control with them. My plugin overcomes a lot of the setbacks that exist with a minimal implementation because even though it appears that you have a timer, you actually don't have a timer object, i.e. you have little to no control over it. With my plugin you get to create real timer objects that can be started, stopped, event handled, they also support adding/subtracted time dynamically etc. My plugin allows you to eliminate ever having to deal with Time.time / Time.deltaTime completely, so you'll be interfacing with something that is common to almost everyone, timers just as you'd expect them to look and act like. A simple solution like you posted may be all that someone needs and if so then that's great, but if the time came to add any other real timer related capabilities, you would have to do some dirty work. My plugin can keep your hands clean and save you many hours of programming and testing. Again, sorry to always have to reply after your posts, but I think once people see how versatile and really easy my plugin is to use, it will open the door to an unlimited number uses. And really the goal is to get developers, young and old, to be able to focus on their application logic and not have to mess around with the Time class at all. Take it easy.
     
  5. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    If you changed the timescale couldn't you just add a multiplier to the timer and that gets around that problem?