Score next to Score!

If you are interested in adding the word “Score” next to your Score (int value), feel free to use the below code in your own games! Be sure to test it to ensure it still fits on your screen with your resolution you set in Text Mesh Pro after adding the extra characters.

    public void IncreaseScore(int amountToIncrease)
    {
        score += amountToIncrease; // make score go up by whatever we pass into it. (score + amountToIncrease)
        // Debug.Log ($"Score is now: {score}"); // testing if score increases - tested works
        scoreText.text = "Score: " + score.ToString();

    }
3 Likes

Looks great! One other option would be to use the string interpolation that we’ve used in the past, like below:

public void UpdateScore(int scoreModifier) {
	playerScore += scoreModifier;
	playerScoreOnScoreboard.text = $@"Score: {playerScore}";
}

In this example you also don’t need to include the ToString() before the closing }, but it wouldn’t hurt if you wanted to add that.

1 Like

You also don’t need the ‘@’ here. It’s used to indicate that the string should be read ‘verbatim’ and helps to avoid a load of escape characters. Like

var folder = @"C:\My Folder\My Sub Folder";
// instead of
var folder = "C:\\My Folder\\My Sub Folder";
2 Likes

Good call. I need it more often then not when I’m coding at work so I’ve just gotten in the habit of always including both, but you’re absolutely right (and I could see it confusing people, for which I apologize).

Awesome! Thanks for the tip!

Privacy & Terms