- Home /
WaitForSeconds Not Working
I am trying to make my score count up with a delay between each one (Like Flappy Bird). I have made the script below but I get no errors. Instead I get this where my score (1, 3, 6, etc) should be:
ScoreKeeper+c__lterator0
I have look for online help (videos and unity answers) but so far I have been unsuccessful. I can post more pices of code if needed.
CountUp Script:
public static IEnumerator CountUp(int score, int counter) {
for (int i = 0; i < counter; i++) {
score++;
yield return new WaitForSeconds (1/counter);
}
}
Where I call this script:
GUI.Label (new Rect (Screen.width / 2 - 175, Screen.height / 3, 100, 50), "Score: " + ScoreKeeper.CountUp(TempScore, 1), MyGuiStyle2);
How are you expecting this to work? It makes no sense to me.
Not familiar with flappy bird, but I can't imagine this logic creating any meaningful scoring mechanism.
The following would create a scoring mechanism to increase the score by 1 every tenth of a second:
// in Score$$anonymous$$eeper script
int score = 0;
public int Score { public get { return score; } }
public void Reset() { score = 0; }
void Start() {
StartCoroutine(CountUp());
}
IEnumerator CountUp() {
while (true) {
score++;
yield return new WaitForSeconds(0.1f);
}
}
// wherever your gui display needs the score
int score = Score$$anonymous$$eeper.Score;
Answer by WillNode · May 15, 2015 at 11:34 AM
counter
is integer number, if you divide an integer number with another integer, then resulting value is always integer (meaning they didn't have any floating numbers).
or in another word, resulting value from 1/int value
will always zero.
public static IEnumerator CountUp(int score, float counter) {
for (int i = 0; i < counter; i++) {
score++;
yield return new WaitForSeconds (1/counter);
}
}