I have the following code in place, and would like to condense the content of ‘Update’ down to a few lines at maximum. The code determines what an on-screen score counter displays, depending on the string length of the score.
using UnityEngine;
public class ScoreCounter : MonoBehaviour
{
public int score;
public GameObject GameManager;
void Update ()
{
score = GameManager.GetComponent<GameLoop>().stats.score;
if(score.ToString().Length == 1)
{
this.GetComponent<TextMesh>().text = "Score: 0000" + score + "000";
}
else if(score.ToString().Length == 2)
{
this.GetComponent<TextMesh>().text = "Score: 000" + score + "000";
}
else if (score.ToString().Length == 3)
{
this.GetComponent<TextMesh>().text = "Score: 00" + score + "000";
}
else if (score.ToString().Length == 4)
{
this.GetComponent<TextMesh>().text = "Score: 0" + score + "000";
}
else if (score.ToString().Length == 5)
{
this.GetComponent<TextMesh>().text = "Score: " + score + "000";
}
}
}
I’ve calculated the number of zeroes needed in the first ‘Score’ string, which is:
5 - score.ToString().Length
I’m fairly sure that the next step here is to implement it as follows:
this.GetComponent<TextMesh>().text = "Score: " + 'insert code here with algorithm calculating zero count' + score + "000";
So the question is, what goes in that space, and if I’m completely wrong, what’s the solution?
Thanks in advance for any help.