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. Dismiss Notice

How can I freeze my timer when I die?

Discussion in 'Scripting' started by taters22, Jun 12, 2014.

  1. taters22

    taters22

    Joined:
    Jun 12, 2014
    Posts:
    9
    I'm currently working on a fps zombie shooter with a timer in the top left corner used as a high score. I want the timer to stop/freeze when I die so that way the clock doesn't keep running after the player dies. This is what i have so far, so what do I need to add?

    #pragma strict
    private var startTime : float;
    var textTime : String;
    //First define two variables. One private and one public variable. Set the first variable to be a float.
    //Use for textTime a string.
    function Start() {
    startTime = Time.time;
    }
    function OnGUI () {
    var guiTime = Time.time - startTime;
    //The gui-Time is the difference between the actual time and the start time.
    var minutes : int = guiTime / 60; //Divide the guiTime by sixty to get the minutes.
    var seconds : int = guiTime % 60;//Use the euclidean division for the seconds.
    var fraction : int = (guiTime * 100) % 100;

    textTime = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
    //text.Time is the time that will be displayed.
    GetComponent(GUIText).text = textTime;

    }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    ... is this a school project or something? we had the exact same code posted yesterday with the exact same question from another new poster... ?

    you'll need to handle the timer in the update() function, put in gamestates to handle "playing" and "gameover" and use the OnGUI() function to simply format and display the timer value.
     
  3. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    private var alive : boolean;

    if(alive)
    {
    var guiTime = Time.time - startTime;
    }




    Also, where did the code tags go?
     
  4. taters22

    taters22

    Joined:
    Jun 12, 2014
    Posts:
    9
    No haha it must be a coincidence, Schools out for me.
     
  5. taters22

    taters22

    Joined:
    Jun 12, 2014
    Posts:
    9
    thanks so much! idk where the tags went lol, it still works though
     
  6. taters22

    taters22

    Joined:
    Jun 12, 2014
    Posts:
    9
    My timer is on the screen and I have a restart menu and would prefer for it to just freeze on screen. Is there a way to do that?