- Home /
Old school score text
Hi!
First of all, if you know the proper name of this, please tell me! I have no idea what it really is called so I chose this title because it is seen in a lot of old games.
Now for the question.
I want to have a score text that works like most of the score texts did in old games like Tetris or Super Mario Bros. Like when the text is always x amount of characters. Example.
Score is 00000000. Then the player gets 100 points. Then it would change to 00000100.
How could I do something like this?
Any help is very appreciated!
I think that using string.format
handles that issue.
Check out this link for examples: http://docs.unity3d.com/ScriptReference/String.html
used string.format
for something very similiar to your issue long time ago so yeah it should work.
Answer by wesleywh · Jul 13, 2015 at 10:23 PM
Just like EmreBdgy suggested you could do it the following way:
Javascript:
GUI.Label(Rect(Screen.width-150, Screen.height-80, 300, 80),"<size=20>"+myScore.ToString("00000000")+"</size>");
C#:
GUI.Label(new Rect(Screen.width-150, Screen.height-80, 300, 80),"<size=20>"+myScore.ToString("00000000")+"</size>");
This way you can have myScore be an int and simply add to it and the ToString will format it for you.
I think you meant to use 0 ins$$anonymous$$d of #
0 - Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
#
- Replaces the "#" symbol with the corresponding digit if one is present; otherwise, no digit appears in the result string.
interesting maccabbe I didn't realize that. That is some important information. I will update my answer to reflect that.
Answer by maccabbe · Jul 13, 2015 at 10:26 PM
In this case the easiest way would be to specify the string format while converting the score from a number to a string.
int score=0;
Debug.Log(score.ToString("00000000"));
score+=100;
Debug.Log(score.ToString("00000000"));
https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx
Answer by nullgobz · Jul 14, 2015 at 01:53 PM
Using string.format is your best bet. Here is a piece of code that dose what you ask:
public class OldtimeScore : MonoBehaviour
{
private int Score;
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
Score += 100;
string scoreWithPadding = string.Format("Score is:{0:00000000}", Score);
print(scoreWithPadding);
}
}
}
Your answer
Follow this Question
Related Questions
Score text wont appear 1 Answer
Score and gui help 1 Answer
coin collecting with onscreen score 1 Answer
How to Check if my Score is Divisible by 10 for More than 1 Frame? 2 Answers