- Home /
Timer not counting down in seconds
I have a counter that did work but now counts down from 100 in around 3 seconds. I need it to count down one very second.
var score = 0;
var timer = 100;
var scoreText = "Score: 0";
var levelToLoad : String;
function Start()
{
}
function Update()
{
timer = timer - Time.deltaTime;
Debug.Log(timer);
if (score < 1)
{
Application.LoadLevel(levelToLoad);
}
}
function OnGUI ()
{
GUI.Label (Rect (10, 10, 100, 20), scoreText + score);
}
function addsometoscore()
{
score += 20;
}
Answer by vexe · Oct 06, 2013 at 08:58 AM
Just tested it out, declare your timer variable explicitly as float and it should work:
var timer : float = 100;
void Update()
{
timer -= Time.deltaTime;
print (timer);
}
If you don't explicitly declare your variable as a float, it will default to System.Int32 which seems to be causing this fast countdown - To prove it:
var timer = 100;
void Start()
{
print (typeof(timer));
}
If you were on C# you'd get an error right away telling you Cannot convert float to int - because:
timer = timer - Time.deltaTime
int = int - float
int = float?
There has to be a match when you assign a value to varible. Not sure how JS even allow this to happen.
It worked to an effect. Only problem is that the whole number counts down per 1 sec. I'm now trying to convert this into a score and the problem reocoors.
Could you elaborate more? - What you mean you're converting it to score? - and yes, it counts per 1 sec, isn't that what you wanted?
I have a score which is currentScore*timer, this is in an Int. this score is displayed on screen and is giving me strange results.
No problem there, just cast timer to an int - in C# it would be score = currentScore * (int)timer; however in JS, see this.
Your answer
Follow this Question
Related Questions
How to realize accurate time counter (millisecond precision) 3 Answers
Timer Continues After Object is disabled 2 Answers
How to subtract 1 from an int each second? 2 Answers
Time Freezer 1 Answer
How to calculate time ? 1 Answer