- Home /
The question is answered, right answer was accepted
How can I add a space between the numbers/digits on my score? (Solved)
Due to the Font type and style I'm using in my game anything written on my GUI has to be spaced apart. (ie SOLVED has to be written as S O L V E D) this was fine until I noticed my score numbers were getting written as normal.
void Update ()
{
// Set the displayed text to be the word "Score" followed by the score value.
text.text = "S c o r e : " + score;
}
Is there anyway to separate the numbers on my score once it reached double figures.
???
Sure! $$anonymous$$ake a generic method that will convert anything to string and add spaces. That's too easy.
'Easy' is such a relative term. Anything is easy 'if' you know how to do it, not so easy if you don't.
Are you kidding?
string str = "something";
for(int i = 0; i < str.Length-1; i++)
str = str.Insert(1 + 2i, " ");
Am I genius or is it really simple?
I really need to sleep, but if you want me to, I can help you later to make a bit more versatile solution for you. :)
Answer by tanoshimi · Feb 15, 2015 at 08:31 AM
I'd use LINQ.
using UnityEngine;
using System.Collections;
using System.Linq; // ADD THIS LINE
public class Whatever: MonoBehaviour {
// Use this for initialization
void Start () {
int score = 4325;
string scoreAsStringWithSpaces = score.ToString().Aggregate(string.Empty, (c, i) => c + i + ' ');
Debug.Log (scoreAsStringWithSpaces); // Outputs 4 3 2 5
}
}
its still not separating the numbers?
the whole script (as I've probably done something wrong)-
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Linq; // ADD THIS LINE
public class Score$$anonymous$$anager2 : $$anonymous$$onoBehaviour
{
public static int score; // The player's score.
Text text; // Reference to the Text component.
void Awake()
{
// Set up the reference.
text = GetComponent <Text> ();
// Reset the score.
score = 0;
}
// Use this for initialization
void Start()
{
int score = 4325;
string scoreAsStringWithSpaces = score.ToString().Aggregate(string.Empty, (c, i) => c + i + ' ');
Debug.Log (scoreAsStringWithSpaces); // Outputs 4 3 2 5
}
void Update()
{
// Set the displayed text to be the word "Score" followed by the score value.
text.text = "S c o r e : " + score;
}
}
???
Are you kidding?!
void Update()
{
// Set the displayed text to be the word "Score" followed by the score value.
text.text = "S c o r e : " + score.ToString().Aggregate(string.Empty, (c, i) => c + i + ' ');
}
The issue is solved now thanks to yourself and @Tanoshimi. Thanks to both you guys, I really do appreciate the help !
:)