Scores that count up to the final score

Hi All,

I have no idea how its called but I want to add a nicer score counter. I have a variable called “score” now every goodie that is won is simply adding lets say 50 to the score instantaneously. Now what I want is that it is adding that 50 slowly to the score so it looks like is counting up within a second or two. I’m looking for a long time to find a tutorial on this but cant find one.

Thanks in advance!

Roy

int score = 0;

IEnumerator Counter(int count)
{
        for(int i = 0; i < count; i++)
        {
                score++;
                 yield return new WaitForSeconds(1/count);
        }
}

It will count it over one second.

Hi BFGames,

Thanks for your answer!

Sorry I forgot to tell its in javascript. So I add the scores from my goodies to count? This is then translating to score? right? just trying to understand this :slight_smile:

Roy

function Do (int count) 
{
    for(int i = 0;i < count; i++)
   {
           score++;
           yield WaitForSeconds (1/count);
   }
}

What you do is call the function, with the amount you want to add to the score. It then runs in the loop adding one to the score each time the loops run, the loop uses waitforseconds which “stops” the function for an amount of time and then runs again.

Aah okay that make sense. But getting some errors from your code.

CE0043: Unexpected token: count.
BCE0044: expecting ;, found ‘i’.
BCE0043: Unexpected token: ).
BCE0044: expecting :, found ‘;’.

I never write in UnityScript, so guess there a quite some mistakes, think its (count : int) if i remember right.

Hi BFGames,

I got it working! thanks for this. This is the js version that worked.

function Do (count : int) {
	for(var i : int = 0;i < count; i++)
		{
			score++;
			yield WaitForSeconds (1/count);
		}
}

Then I use sendmessage to call the function with a int number hope is the correct way to do this. If there is a better way please tell me :slight_smile:

public GameObject coinManager;
	
	void Start(){
		coinManager = GameObject.Find("CoinManager");
	}

coinManager.SendMessage("Do", 50);

/Roy