- Home /
Clock, numbers past 2 digits????
My problem is that i cant seem to get my timer/clock/counter to get past the 2 digits... OK so example once you input a number for example 9 you only use 1 digit , after 9 comes 10 (2 digits 1 and 0 ).....I can get up to two digits, but i don't know how to get to numbers with 3 digits or more ...like 100...
#pragma strict
var TotalPoints : int;
var CoinsTextTexture: Texture;
var fontSize : int = 20;
var numbers : Texture2D[];
var digit1 : int;
var digit2 : int;
var digit3 : int;
function Update(){
digit1 = TotalPoints%10;//flips after 10
digit2 = Mathf.Floor(TotalPoints*.1);//changes every 10
digit3 = // how do i do this one???!!!!! and make digit2 flip back to 0 after reached 10
}
function OnGUI(){
GUI.DrawTexture(Rect(Screen.width*0.7,Screen.height*0.01,190,100), CoinsTextTexture);
GUI.DrawTexture(Rect(Screen.width*0.9,Screen.height*0.01,100,100), numbers[digit1]);
if(digit2>=1){
GUI.DrawTexture(Rect(Screen.width*0.85,Screen.height*0.01,100,100), numbers[digit2]);
}
}
Answer by JCX · Feb 02, 2014 at 11:58 PM
if(digit2>10)
{
digit2-=10;
digit3++;
}
or
digit3 = Mathf.Floor(TotalPoints*.01);
or you can convert it to string and each letter convert back to int
string s = TotalPoints.toString()
digit1 = (int)s[2];
digit2 = (int)s[1];
digit3 = (int)s[0];
how can i convert it to string and back? can you show me an example please? by the way thank you for the reply
i don't know how in unity script but looks like it's same string as in c# http://docs.unity3d.com/Documentation/ScriptReference/String.html http://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx
i get errors when i try this .. aside from the missing ; after toString()....i get missing semicolon errors on:..... how come?
digit1 = (int)s[2];
digit2 = (int)s[1];
digit3 = (int)s[0];
Prbabably I have to convert it to java script ....but im not sure how to .. I dont understand what (int)s[0];
is doing
this works:
var TotalPoints:int = 123;
var s:String = TotalPoints.ToString();
Debug.Log(s);
var digit1:int = int.Parse(""+s[0]);
var digit2:int = int.Parse(""+s[1]);
var digit3:int = int.Parse(""+s[2]);
Debug.Log(digit1);
Debug.Log(digit2);
Debug.Log(digit3);
dunno better way then adding char to strign right now, i use c# not unityscript
Answer by GTX Titan · Feb 03, 2014 at 12:48 AM
Okay time for magic:
digit3 = Math.Floor(TotalPoints/100);
digit2 = Math.Floor((TotalPoints-digit3*100)/10);
digit1 = Math.Floor((TotalPoints-digit3*100)-digit2*10);
sorry but this gives a negative number if its on the first digit
It can't give negative... if there is no digit3 digit3 = 0 and when you do 0*100 and subtract with it...
Believe me this is as simple as it gets and it works i tested.
For number 9 it gets these: digit3 = 0 digit2 = 0 digit1 = 9 For number 21 it gets these: digit 3 = 0 digit2 = 2 digit1 = 1
I have "extended" mathemathics in high school and i'm getting straight A's with no effort whatsoever :)