- Home /
question about score counter
hi guys i have script that shows score. here it is:
function show(){
HowMuchToWait = 0.1;
addScore();
}
function addScore(){
for(effectScore=0; effectScore<=mascore; effectScore+=1){
scoreText.text=effectScore.ToString();
yield WaitForSeconds(HowMuchToWait);
}
}
it works properly but there is 1 mistake as i think. if ur score will be 1000000000 u have to wait a lot of time, so i need thing like that: if ur score is 50 u will wait for example 2 secs, if u have 1000000 same u have to wait 2 secs, any ideas? or is it dangerous that even if i make it work correctly comp will crash? XD thanks for any help
$$anonymous$$y understanding of the code is this: When the function show()
is called, the variable How$$anonymous$$uchToWait
is set to a value and addScore()
is called. In the for loop in addScore()
, you're starting from 0 and adding 1 to effectScore
every time until it is less than mascore
, which I assume is the score that you eventually want effectScore
to be equal to. But every time effectScore
is incremented, you must wait How$$anonymous$$uchToWait
. This is where the problem is, I think, because you are waiting a length of time every time the score is incremented, ins$$anonymous$$d of having a fixed time to go from 0 to mascore
.
Answer by JSierraAKAMC · May 30, 2015 at 07:24 PM
I created a script that does what I think you were trying to do here. #pragma strict
var mascore : int = 0;
var effectScore : int = 0;
var HowMuchToWait : float = 0.1;
//How long the entire process takes
var waitTime : float = 2;
private var currentTime : float = 0;
private var hasCompleted : boolean = true;
var scoreText : TextMesh;
function Update () {
if(Input.GetButtonDown("Fire1")){
mascore += 100;
show();
}
//New way:
//If it isn't complete,
if(!hasCompleted){
currentTime += Time.deltaTime;
//Start incrementing the currentTime by the time since the last frame
scoreText.text = Mathf.FloorToInt(Mathf.Lerp(0, mascore, currentTime/waitTime)).ToString();
//Set the text
if(currentTime >= waitTime){
currentTime = 0;
hasCompleted = true;
}
}
}
function show(){
HowMuchToWait = 0.1;
//Old way:
//addScore();
//New way:
hasCompleted = false;
}
//Old way:
function addScore(){
for(effectScore = 0; effectScore <= mascore; effectScore++){
scoreText.text = effectScore.ToString();
yield WaitForSeconds(HowMuchToWait);
}
}
Your answer
Follow this Question
Related Questions
Uniting 2 integers from two different sources 1 Answer
Make a custom score counter in unity with c# 1 Answer
Connecting 2 scripts(javascpt)? 2 Answers
how to keep track of bricks broken 1 Answer