- Home /
How to break up a number into seperate digits?
Say I have a number that I want to be used more like a code than a number. For example; I could have the number 459, where 4 is the level that the player is on, 5 is the player's health, and 9 is the player's number of coins. At the end of each game, the player would be given a code like this that they could type in again and go back to where they left off (almost like PlayerPrefs that can be used on different computers). However, I don't know how I would be able to separate the integer "459" into the three integers "4","5", and "9". Is there a way to do this?
Answer by tanoshimi · Jun 04, 2014 at 10:16 PM
From your description, I wouldn't treat 459 as an integer, but as a string "459". You can then access each char in the string as my string[0], my string[1] etc.
Thanks! I didn't know you could make a string act like an array with each character.
Answer by DarKTower · Jun 04, 2014 at 11:00 PM
Another possibility if you are wanting to use more than single digits (In which Tanoshimi's method would not work), you could use a String but split the numbers up by commas, periods, slashes, |'s, whatever you prefer. This is my preferred method for storing data like this, as it's easy to use, read, and write.
From there you just Split() using whatever breaker you used, and then use the array from there.