How to make a score label increment from 0 to total score

Hi,

I have a label with a score in my score screen, and I want this to show the score at 0 initially and take a specified period of time to rise to the actual score value. This should show the score incrementing.

I want the time to be constant and independent from the score, go up to 2000 score should take the same specified time as going up to 500000.

Any help would be appreciated.

Best Regards.

using UnityEngine;
using System.Collections;

public class ScoreCounter : MonoBehaviour 
{
	public float duration = 0.5f;
	int score = 0;
	
	void OnGUI () {
		GUIButtonCountTo (0);
		GUIButtonCountTo (5000);
		GUIButtonCountTo (20000);
		GUILayout.Label ("Current score is " + score);		
	}

	void GUIButtonCountTo (int target) {
		if (GUILayout.Button ("Count to " + target)) {
			StopCoroutine ("CountTo");
			StartCoroutine ("CountTo", target);
		}
	}
	
	IEnumerator CountTo (int target) {
		int start = score;
		for (float timer = 0; timer < duration; timer += Time.deltaTime) {
			float progress = timer / duration;
			score = (int)Mathf.Lerp (start, target, progress);
			yield return null;
		}
		score = target;
	}
}

in JS:

var increment : int;
var start : int;
var end : int;
var now : int;
var time : float;

function Start()
{
	Increment_Points();
	start = 0;
	now = start;
}

function Increment_Points()
{
	while(true)
	{
		yield WaitForSeconds(time);
		if(now < end)
		{
			now += increment;
		}
		else
			break;
	}
}

let me know if you need it in C#

This was my solution to this, I left the time issue:

	public int partialScore;
	private int scoreT = 0;

	void Start () {
		partialScore = 3000; //example score
	}

	void Update () {		
		if(int.Parse(GetComponent<UILabel>().text) < partialScore){
			GetComponent<UILabel>().text = scoreT.ToString();
			scoreT = scoreT + 10; //Example Step
		}
	}

The step size could be done by simple math, but since scores aren’t that are usually within the range of 0-4000, this take about 3 seconds, which is OK.

Thanks guys.