I’ve gone ahead and used some of the string tools in C# to pre-format my score text, so that it always displays the same number of digits.
I started off with a string of 6 0s, and every time a hit is registered, it removes the appropriate number of characters from the end of the string and puts the score at the end.
Working with strings is always a pain in the butt, but I’m glad I got it working
TMP_Text scoreText;
int score;
[SerializeField] string scoreBuffer = "000000";
int cutAmount;
private void Start(){
scoreText = GetComponent<TMP_Text>();
}
public void IncreaseScore(int amountToIncrease){
score += amountToIncrease;
scoreText.text = cutAndClampScore(score.ToString());
}
string cutAndClampScore(string scoreString){ //making sure that the score buffer never has less than zero characters.
if (scoreString.Length > scoreBuffer.Length) {
cutAmount = scoreBuffer.Length;
} else {
cutAmount = scoreString.Length;
}
return scoreBuffer.Remove(1,cutAmount) + scoreString;
}
I do have a question, though. When I was working on it, I originally wanted to initialize the “cutAmount” variable inside the if statements within cutAndClampScore(). However, Visual Studio told me that cutAmount does not exist in the current context. Why does it not allow you to do that?
For reference, this is what that method looked like before I changed it to initialize on the class level.
string cutAndClampScore(string scoreString){ //making sure that the score buffer never has less than zero characters.
if (scoreString.Length > scoreBuffer.Length) {
int cutAmount = scoreBuffer.Length;
} else {
int cutAmount = scoreString.Length;
}
return scoreBuffer.Remove(1,cutAmount) + scoreString;
}