I’m trying to make a simple timer that quits the game after a certain amount of time. But for some reason, I keep screwing up, and I can’t seem figure out what’s wrong. I guess today’s not my day. Or I’m just not good at this.
This is what I got so far:
static var timeLimit = 1;
function Awake
{
var timeLeft : float = timeLimit * 60;
}
function Update ()
{
if ( timeLeft <= 0 )
{
Application.Quit();
}
}
Apart from in Awake, you are not modifying timeLeft in any way, therefore it is not doing anything but storing the value 60 .
You want to use timeLeft as a counter, therefore subtract time from it in each frame :
timeLeft -= Time.deltaTime;
Now timeLeft should countdown (in real time).
Edit : also, I now see that you have declared the variable in Awake, this is incorrect… Here is an example of your script but working :
static var timeLimit : float = 1; // why is this static? for other scripts to access?
private var timeLeft : float;
function Awake
{
timeLeft = timeLimit * 60;
}
function Update ()
{
timeLeft -= Time.deltaTime;
if ( timeLeft <= 0 )
{
Application.Quit();
}
}