Search Unity

Adding Time to a Timer

Discussion in 'Scripting' started by bfranklin, Nov 5, 2007.

  1. bfranklin

    bfranklin

    Joined:
    Oct 18, 2007
    Posts:
    14
    I'm trying to write a script that will add 30 seconds to a timer that is constantly counting down.

    This is my timer script that is attached to a GUI Text:

    Code (csharp):
    1. var startTime = 30.0;
    2.  
    3. function Update () {
    4.     //The time left for player to complete level
    5.     timeLeft = startTime - Time.time;
    6.     //Don't let the time left go below zero
    7.     timeLeft = Mathf.Max (0, timeLeft);
    8.     //Format the time nicely
    9.     guiText.text = FormatTime (timeLeft);
    10. }
    11.  
    12. //Format time like this
    13. //12(minutes):34(seconds).5(fraction)
    14.  
    15. function FormatTime (time) {
    16.     var intTime : int = time;
    17.     var minutes : int = intTime / 60;
    18.     var seconds : int = intTime % 60;
    19.     var fraction : int = time * 10;
    20.     fraction = fraction % 10;
    21.    
    22.     //Build string with format
    23.     //12(minutes):34(seconds).5(fraction)
    24.    
    25.     timeText = minutes.ToString () + ":";
    26.     timeText = timeText + seconds.ToString ();
    27.     timeText += "." + fraction.ToString ();
    28.     return timeText;
    29. }
    I would like 30 seconds to be added to the timer whenever the game object "acorn" is destroyed.

    This is the code attached to the acorn to destroy it when the player is within the trigger collider and presses 1:

    Code (csharp):
    1. var acorn : GameObject;
    2. var score : Score;
    3.  
    4. function OnTriggerStay(col : Collider) {
    5.     if (Input.GetKey("1")) {
    6.         score.AddToScore ();
    7.         Destroy (acorn.gameObject);
    8.     }
    9. }
    I assume there is a line of code that can be added after "Destroy (acorn.gameObject);" to add time to the timer, but I've been unsuccessful so far.

    Any help is g reatly appreciated.

    Thanks,
    Brian
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Can't you just write something like:

    Code (csharp):
    1. timer.startTime += 30.0;
     
  3. bfranklin

    bfranklin

    Joined:
    Oct 18, 2007
    Posts:
    14
    I'm sorry, I don't really understand what you mean.

    If I add "timer.startTime += 30.0;" below "Destroy (acorn.gameObject);"

    I get an error of Unknown identifier 'timer'.

    I'm quite new to Unity and scripting so the more detailed you can be the better.
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Your script should look like this:

    Code (csharp):
    1. var acorn : GameObject;
    2. var score : Score;
    3. var timer : Timer;
    4.  
    5. function OnTriggerStay(col : Collider) {
    6.    if (Input.GetKey("1")) {
    7.       score.AddToScore ();
    8.       Destroy (acorn.gameObject);
    9.       timer.startTime += 30.0;
    10.    }
    11. }
    Be sure that the name of your timer script is Timer
     
  5. bfranklin

    bfranklin

    Joined:
    Oct 18, 2007
    Posts:
    14
    Hey thanks. That worked perfectly.
     
  6. mehowe7

    mehowe7

    Joined:
    Dec 30, 2009
    Posts:
    65
    im using a similar script but having problems and wondered if you could help. script is:

    var startTime = 30.0;
    var minutes : int = 0;
    var seconds : int = 20;
    var fraction : int = 0;

    function Update () {

    //The time left for player to complete level
    timeLeft = startTime - Time.time;
    //Don't let the time left go below zero
    timeLeft = Mathf.Max (0, timeLeft);
    //Format the time nicely
    guiText.text = FormatTime (timeLeft);
    }

    //Format time like this
    //12(minutes):34(seconds).5(fraction)

    function FormatTime (time) {
    var intTime : int = time;
    var minutes : int = intTime / 60;
    var seconds : int = intTime % 60;
    var fraction : int = time * 10;
    fraction = fraction % 10;

    //Build string with format
    //12(minutes):34(seconds).5(fraction)

    timeText = minutes.ToString () + ":";
    timeText = timeText + seconds.ToString ();
    timeText += "." + fraction.ToString ();
    return timeText;
    }

    if (timeLeft <= 0.0) {
    Application.LoadLevel ("FixTheBridge");
    }


    i get the error unknown identifier 'timeLeft'

    any ideas?
     
  7. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    "TimeLeft" is never declared, but you're using it in Update(). Add
    Code (csharp):
    1. var timeLeft = startTime;
    up near the top with your other variable declarations. It will declare the timeLeft variable and give it the same initial value as starTime;

    Also, please use the code tags on the forum when posting code; it really helps when reading code on the forum.
     
  8. etomp10291

    etomp10291

    Joined:
    Jan 11, 2010
    Posts:
    108
    This was a great help...thanks!

    One question, how could i modify so that when an object was picked up it added the time?? (Sorry i am a nooobie)

    Thanks for your help!!
     
  9. BeeVee

    BeeVee

    Joined:
    Jul 11, 2011
    Posts:
    16
    Noobie here as well. I'm trying the same thing as the OP but I get "Object reference not set to an instance of an object
    On_touch2.OnTriggerEnter () (at Assets/labU/Scripts/On_touch2.js:18 )"

    I have two scripts, one for the timer, which is called barClock:
    Code (csharp):
    1.  
    2.  
    3. function Awake () {
    4.         DontDestroyOnLoad (this);
    5.         clockFGMaxWidth = clockFG.width;
    6.  
    7.     }
    8.  
    9. var clockIsPaused : boolean = false;
    10. static var startTime : float; //(in seconds)
    11. var timeRemaining : float; //(in seconds)
    12. var percent : float; //for bar clock
    13. var clockBG : Texture2D; //for bar clock
    14. var clockFG : Texture2D; //for bar clock
    15. var clockFGMaxWidth : float;  //starting width of the FG bar for bar clock
    16. var nextLevel : int;
    17.  
    18. function Start()
    19. {
    20.     startTime = 600.0; //time for the whole game
    21.     timeRemaining = startTime;
    22. }
    23. function Update() {
    24.     if (!clockIsPaused)
    25.     {
    26.         // make sure the timer is not paused
    27.         DoCountdown();
    28.     }
    29. }
    30.  
    31. function DoCountdown()
    32. {
    33.     timeRemaining -= Time.deltaTime;
    34.     percent = timeRemaining/startTime * 100; //for bar clock
    35.     if (timeRemaining < 0)
    36.     {
    37.         timeRemaining = 0;
    38.         clockIsPaused = true;
    39.         TimeIsUp();
    40.     }
    41.     ShowTime();
    42. }
    and one called On_touch2 that looks like this:

    Code (csharp):
    1.  
    2. var score:GameObject;
    3. var barClock : barClock;
    4.  
    5. function Start()
    6. {
    7.     score = GameObject.Find("Score");
    8. }
    9.  
    10. function Update () {
    11.  
    12. }
    13.  
    14. function OnTriggerEnter() {
    15.     transform.parent.gameObject.audio.Play();
    16.     Destroy(gameObject);
    17.     ++scoreScript.score;
    18.     barClock.timeRemaining +=200.0;
    19. }
    20.  
    This is the last bit of my game I need help with, after that it's tidying up, refactoring the code and adding levels. Please help :)

    B
     
  10. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    Where exactly do you reference barClock? I see you put a variable for it... but where do you assign it....
     
  11. BeeVee

    BeeVee

    Joined:
    Jul 11, 2011
    Posts:
    16
    Not sure I understand? Not trying to be thick, I *am* thick :) I've used functions from other scripts before for my game by putting a var at the top of the script that wants to reference and then the function from the referenced script works (like so: ScriptA contains functionX. ScriptB sets a var of scriptA:ScriptA and then later calls a function from ScriptA by stating ScriptA.FunctionX) is that not right?

    B
     
  12. BeeVee

    BeeVee

    Joined:
    Jul 11, 2011
    Posts:
    16
    BTW. This error doesn't stop the game running, it just pops an error each time you hit a coin. Not like usual where you can't hit play until you've corrected your grievous coding sin.

    B
     
  13. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    That should say programming. I would suggest you go do some basic coding tutorials. Your error is un-related to Unity, and more specifically coding basics.

    The answer to your problem is initialtime needs to be a public variable. Im not sure you'll understand what I mean, but thats the problem.