- Home /
ScoreBoard Counter Problems
I'm racking my brains at this and I am guessing I am over looking the simple solution. Searching for answers and haven't found one that works with what i am trying to do.
I have a score counter, it is using UI.Images for each number. Tried different methods which all turned into errors for me.
I have a Index of 0-9 sprites, and 6 "sections" that need to change.
Basically, if the score is 980345, it needs to display from 0 to 5 in the sections.
scoreUIImage0.sprite = ui_Numbers[playerman.playerScore % 10];
Using that script will make the "0" slot work. But breaks for the rest of the slots. I am probably not using it correctly
No, The reason its going right to left is how its displayed on on the hud. Basically, I have the score int. I just need to be able to pick each number from the int.
So, if the score is 123456. each number will go in each slot compared to the position. I need to get each number from the int and use them to display in the correct slot the correct number.
I could do this easily by using Text. But I want to be able to use a image. I already have it working for single digits.
Hopefully I am explaining this well enough.
scoreUIImage0.sprite = ui_Numbers[(playerman.playerScore/1) % 10];
scoreUIImage1.sprite = ui_Numbers[(playerman.playerScore/10) % 10];
scoreUIImage2.sprite = ui_Numbers[(playerman.playerScore/100) % 10];
scoreUIImage3.sprite = ui_Numbers[(playerman.playerScore/1000) % 10];
...
?
:faceplam: Works perfect, and doesn't take many lines. I knew it was simple. I just couldn't picture what I needed to do. Thanks and if you add it as a answer I will accept it.
Answer by veyseler · Jul 13, 2021 at 07:17 AM
var str = playerman.playerScore.ToString()
var len = str.Length;
var int0 = Int32.Parse(str[len - 1].ToString());
scoreUIImage0.sprite = ui_Numbers[int0];
You can apply similar approaches for the rest of the sprites. You are taking mod base 10 so its only working for the last digit. If you wanna go with the math approach you need the divide that playerScore by 10 at each step. Like this you can use the lastDigit as your indexing variable:
var tempInt = 1234;
while (tempInt != 0)
{
int lastDigit = tempInt % 10;
tempInt = tempInt / 10;
}
Thanks it works like this too! but was trying to keep lines and vars to a $$anonymous$$imum already. And the int to string is what I was trying to do at first and kept getting errors about the array.
Your answer
![](https://koobas.hobune.stream/wayback/20220613040159im_/https://answers.unity.com/themes/thub/images/avi.jpg)