- Home /
How can I decrease points by the second?
In my game, I have coded a variable,
public float points;
, that I would like to be decreased from 1000 for every second since the game starts. The game will start with the points at 1000, and decrease endlessly until the player reaches the end of the game. How can I do this?
Answer by DoctorMoney · Feb 23, 2014 at 02:23 AM
You would have to use Time.time (the current time) to determine when a second has passed.
When a second has passed just do points -= 1;
I'd provide an example but I only know UnityScript so hopefully it helps.
var timeSecond : float = 1; //variable in which it is the Time.time variable plus 1
var points : float = 1000;
function Update () {
if (Time.time >= timeSecond){ //if the current time is greater than or equal to the one second mark
points -= 1; //set points to current value - 1
timeSecond += 1; //set 1 second past time to current value + 1
}
}
This should loop and work, I haven't tested it though and it's not in C#.
Hope i helped
Answer by getyour411 · Feb 23, 2014 at 02:20 AM
As you have written it the game would be over in one second. Regardless, something like
Start() {
// setup stuff
StartCoroutine(decreaseScore());
}
IEnumerator decreaseScore() {
while (score > 1) {
score = score - 1000;
yield return new WaitForSeconds(1);
}
// call GameOver
}
Edit: replace 'score' with 'points', just noticed you were using that variable name
Your answer
Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
Start timer with trigger 1 Answer
How would you make your player freeze for a certain amount of time? 2 Answers
OpenFeint float scores 2 Answers
Changing a float number by time 1 Answer