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;
}
vexe
2
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.