- Home /
How do I create a mission timer?
I have been remaking an old game that I love, but I have one problem... I can't make a timer, not a countdown timer, but a timer that counts up... I am familiar with Unity, but not good at programming... thanks if anyone helps.
Answer by Mmmpies · Jan 04, 2015 at 05:56 PM
I tend to agree with @tanoshimi that you should at least try something first but this is so simple it's almost easier to answer than ask what you've tried.
private float myTimer;
Update()
{
myTimer = myTimer + Time.deltaTime;
}
Time.deltaTime is 1 second divided by the number of frames you get on the platform you run your game on. No matter what your platform 1 second will = 1 second real time if you add Time.deltaTime to it each frame as Update does.
Example in C#
Answer by BlackPanda · Jan 04, 2015 at 06:11 PM
Or you can use the Stopwatch class:
using UnityEngine;
using System.Collections;
using System.Diagnostics;
public class MissionTimer
{
StopWatch watch = new Stopwatch();
int maxTime = 15;
void Start()
{
watch.Start();
}
void Update()
{
if(watch.Elapsed.Seconds > maxTime)
{
watch.Stop();
print("Time's UP!");
}
}
}
Again, you should first google basic programming problems like this before posting here. :)
Your answer
Follow this Question
Related Questions
How to make this display milliseconds? 3 Answers
How do I stop 'timeSinceLevelLoad' without pausing entire game? 2 Answers
Timer reset 1 Answer
Timer Pause and Reset Issue 2 Answers
Unity 5 - Time counter Up script (millisecond precision) UI 1 Answer