Scripting

Updated on July 27, 2022

Public methods

There are several public methods that can be called from the Timer script.

First of all, make sure to get a reference to the Timer class in your preferred method.

double GetRemainingSeconds()

This will return the number of remaining seconds. For example, you can make a check every second to see if the remaining time is equal to halfway through.

//Console logs the remaining seconds
double secondsLeft = myTimer.GetRemainingSeconds();
Debug.Log(secondsLeft);

StartTimer()

This method simply starts the timer, with the time set in the inspector. You have to stop the timer or wait for it to end before calling this method again.

//Starts the timer after 5 seconds
myTimer.Invoke("StartTimer", 5f);

StopTimer()

This will stop the timer from running, and reset its values according to the count method. If counting up, your reset value will be 0, if counting down, your reset value will be the values set in Set time.

//Stops the timer if there is less than 5 seconds remaining
//Checks every 0.25 of a second.
void Start() 
{
    InvokeRepeating("CheckTimer", 0, 0.25f)
}
private void CheckTimer() 
{
    if(myTimer.GetRemainingSeconds() < 5) 
    {
        myTimer.StopTimer();
    }
}

//Results in the timer being stopped and reset to the time set in the inspector

PauseTimer()

Pauses the timer, stopping all counting without resetting anything. If the timer is paused, you can either resume or stop the timer. Please note you cannot start a new timer while it is paused.

//Pauses the timer if key "p" is pressed
void Update()
{
    if(Input.GetKeyDown("p"))
    {
        PauseTimer();
    }
}

Note – This is a paid feature.

ResumeTimer()

Resumes the timer if it was paused. This method only works if the timer is paused.

//Resumes the timer if key "r" is pressed
void Update()
{
    if(Input.GetKeyDown("r"))
    {
        ResumeTimer();
    }
}

Note – This is a paid feature.

AddTime(float seconds)

Add time to the timer in seconds. You can use the inspector Set time section to work out how many seconds to add. This will only work if the time to add will not take you beyond the Set time if counting down. Please note, this method by its nature works in reverse when you change the count method. For example, trying to add time to a timer counting down from 10 seconds, will make the timer last longer. When adding time to counting up, it will make the timer end quicker.

//Adds more time if the level has been won. 
bool levelWin = false;
int score = 0;
void Update()
{
    if(score >= 40) 
    {
        levelWin = true;
        score = 0;
    }
    if(levelWin)
    {
        AddTime(60);
        levelWin = false;
    }   
}
void addScore() 
{
    score++;
}

Note – This is a paid feature.

RemoveTime(float seconds)

Resumes the timer if it was paused. This method only works if the timer is paused.

//Removes time if you lose the level.
bool levelWin = false;
int score = 0;
void Update()
{
    if(score < 40) 
    {
        levelWin = true;
        score = 0;
    }
    if(levelWin)
    {
        RemoveTime(30);
    }   
}
void addScore() 
{
    score++;
}

float ReturnTotalSeconds()

This will return the number of seconds set in the inspector under Set time. This does not count, it simply returns the total number of seconds set.

//If the timers current seconds left are less than the seconds set in the inspector.
if(ReturnTotalSeconds() > GetRemainingSeconds()) 
{
    Debug.Log("Timer has started");
}

double ConvertToTotalSeconds(float days, float hours, float minutes, float seconds)

You can call this function with your desired days, hours, minutes and seconds to receive these values returned as seconds.

//Displays 1 day, 14 hours, 10 minutes and 37 seconds in seconds. 
double seconds = ConvertToTotalSeconds(1, 14, 10, 37);
Debug.Log(seconds);

Note – For the free version, you can call this function without the days parameter.

string DisplayFormattedTime(double remainingSeconds)

This method handles the display input and output. If you put a total number of seconds into it, it will convert it and return a string of the selected display options set in the inspector. This method is used in conjunction with the remaining seconds to output the remaining time in a text format.

//Displays a string of your remaining time using the display options set in the inspector. 
string timeLeft = DisplayFormattedTime(GetRemainingSeconds());
Debug.Log(timeLeft);

Still need help? Contact support at support@golemitegames.com

Please describe your issue in detail and if possible, provide screenshots to help us understand your problem better and answer your question more effectively.