Creating a timer that is triggered by a boolean

I have used this site to create my timer and most of it works.

I used the following code

private var startTime;
private var restSeconds : int;
private var roundedRestSeconds : int;
private var displaySeconds : int;
private var displayMinutes : int;

var countDownSeconds : int;

function Awake() {
    startTime = Time.time;
}

function OnGUI () {
    //make sure that your time is based on when this script was first called
    //instead of when your game started
    var guiTime = Time.time - startTime;

    restSeconds = countDownSeconds - (guiTime);

    //display messages or whatever here -->do stuff based on your timer
    if (restSeconds == 60) {
        print ("One Minute Left");
    }
    if (restSeconds == 0) {
        print ("Time is Over");
        //do stuff here
    }

    //display the timer
    roundedRestSeconds = Mathf.CeilToInt(restSeconds);
    displaySeconds = roundedRestSeconds % 60;
    displayMinutes = roundedRestSeconds / 60; 

    text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds); 
    GUI.Label (Rect (400, 25, 100, 30), text);
}

however it starts automatically when the game starts and not when the boolean becomes true. If I place the startTime within a boolean check then it never comes up and the game reads it as 0

I know I should have mentioned i am slightly new to unity. It is for a game I am making for school. All of the other factors of it work it is only making the time count down from a specific point ( boolean = true) instead of counting down from game start which it is doing

Okay, then I’ll let you do most of the work.

Here is a hint to start you off:

[Edit: Much harder than I thought to give you help while not giving you the answers. Here is the Structure I’d use:]

//First, use a single variable (pun!) to represent countDownTime

//Every FRAME I would subtract that by the time since last frame. (Use Update()).

//Once you have this working, it should be easy to add in the boolean switch.

Edit:

Also note, you need to know the difference between INT and FLOAT(single).

Though i am new to using unity, but not new to coding, that is extremely vague… I am thankful for your help, however i have only a day to get it working as well as some other aspects to finalize it before presenting on Friday. As i said every aspect of the time works perfectly except for it starting to count down when i want it to, instead of always when the game first starts. Once i get that everything else will be done.

Okay i’ll talk you through it:

This is how you write a simple Count Down.

var timeRemaining : float = 5.0;  // keep it public so that you can change the value in the editor.

function Update()
{
   timeRemaining -= Time.deltaTime;
}

Step 1: Rewrite your code so it works like this - don’t so the subtraction in the OnGUI.

[hint: I think you can replace 2-3 variables with this single variable - KISS]

Step 2: Fix up your math, you’ve got integers and floats muddled together. I’d change everything before Mathf.CeilToInt() with floats.

[Note:‘==’ does not work with floats you need to use > or <or <= or >= IIRC.]

Step 3: Now that you’ve got your code working put in the missing if (running) statement.

[Hint: it should surround the only line in your Update() function.]

Thanks man lol,

Though it was very vague i did figure it out in the end. I basically aded what you gave me to start, commented out the stuff i was using, then tested what you gave me first, then looked how what i had could use the info and now it works perfectly. i just need to get my checks based on time left finished and i am set.

so again i say thanks NPSF3000!

Normally I would have just done it for ya, but you have to learn somehow :slight_smile:

Hey it is good either way, i am getting three books form the same series as my unity book in preperation for an intership at the end of my corse