- Home /
deactivate un-used zeros
I have a script which counts with textures for example 123 would assign the texture 1,2 and 3 to be rendered, but when I only have 1 digit like (5)the others are set to zero making it look like (500)....so how can i disable the zeros until they become meaningful like in numbers (20) or (560)...
// varibles begin //
var TotalPoints:int; // this variable is edited externaly and shows howmany points we have
//***************************
var digit1:int;
var digit2:int; // all these are to store the integers
var digit3:int;
var digit4:int;
//***************************
var CoinsTextTexture: Texture; // texture section this is for the text "coins"
var numbers : Texture2D[];// array of images of numbers 0-9
//***************************
//end of variables//
function Update(){
var s:String = TotalPoints.ToString();
digit1 = int.Parse(""+s[0]);
digit2 = int.Parse(""+s[1]);
digit3 = int.Parse(""+s[2]);
digit4 = int.Parse(""+s[3]);
}
function OnGUI(){
GUI.DrawTexture(Rect(Screen.width*0.0,Screen.height*0.75,70,50), CoinsTextTexture);
GUI.DrawTexture(Rect(Screen.width*0.0,Screen.height*0.85,100,100), numbers[digit1]);
GUI.DrawTexture(Rect(Screen.width*0.04,Screen.height*0.85,100,100), numbers[digit2]);
GUI.DrawTexture(Rect(Screen.width*0.08,Screen.height*0.85,100,100), numbers[digit3]);
GUI.DrawTexture(Rect(Screen.width*0.12,Screen.height*0.85,100,100), numbers[digit4]);
}
Please remove massive amounts of whitespace from the question. Theres no need to leave 5 lines between a function call and the closing bracket.
Also if you're already parsing digits, why not just parse the whole number as one thing and have each character as a digit.
If I was to parse the whole number i would get false numbers because of the zeros ....... i have 4 digits ( 0 0 0 0 ) if i was to parse a six, i would get this ( 6 0 0 0 ).... now that appears to be 6000 but its just 6. I tried to parse from right to left like (0 0 0 6) but its hard because once you begin counting to more digits for example 15 ( 0 0 1 5) the code actually reads ( 0 0 5 1)....if i swap them single digit # would be in the wrong place:(
You're approaching the whole thing from the wrong direction. There is absolutely no need for you to treat every digit separately.
var number:int;
var numbers:Texture2D[];
//then
var str = number.ToString();
for (var character:char in str) {
var num = parseInt(character);
Debug.Log(numbers[num])
}
This is really just a 10-20 liner, not some huge piece of code
its telling me I cant convert char to string and that primitive char cant be refined..
its probably telling me that it cant convert it to string because str is a string and then you re- specify that its a string ...
Answer by darkcookie · Feb 05, 2014 at 10:34 PM
After a long struggle i finally got it ... This is not the most efficient way but it worked. If any one has any ideas of how i can make it better or if I'm doing this wrong please tell me. here is the code in case someone has this problem! :]
// varibles begin //
var TotalPoints:int; // this variable is edited externaly and shows howmany points we have
//***************************
var digit1:int;
var digit2:int; // all these are to store the intigers
var digit3:int;
var digit4:int;
//***************************
var s: String; //this holds the current number
//***************************
var CoinsTextTexture: Texture; // texture section this is for the text "coins"
var numbers : Texture2D[];// array of images of numbers 0-9
//****************************
//end of variables//
function Update(){
s = TotalPoints.ToString();
var numberInt: int;
numberInt = int.Parse(s);
digit1 = int.Parse(""+s[0]);
if(numberInt>9){ // I added this condition becuase i got the index is out of range error..
digit2 = int.Parse(""+s[1]);
}if(numberInt>99){
digit3 = int.Parse(""+s[2]);
}if(numberInt>999){
digit4 = int.Parse(""+s[3]);
}
}
function OnGUI(){
GUI.DrawTexture(Rect(Screen.width*0.0,Screen.height*0.75,70,50), CoinsTextTexture);// random texture display
//Digit 1
//****************************************************************************************************************
if(s.Length==1){ // if s has 1 digit ************************************
GUI.DrawTexture(Rect(Screen.width*0.0,Screen.height*0.85,100,100), numbers[digit1]);
}
//Digit 2
//****************************************************************************************************************
if(s.Length==2){ // if s has 2 digits ****************************
GUI.DrawTexture(Rect(Screen.width*0.0,Screen.height*0.85,100,100), numbers[digit1]);
GUI.DrawTexture(Rect(Screen.width*0.04,Screen.height*0.85,100,100), numbers[digit2]);
}
//Digit 3
//****************************************************************************************************************
if(s.Length==3){ // if s has 3 digits ****************************
GUI.DrawTexture(Rect(Screen.width*0.0,Screen.height*0.85,100,100), numbers[digit1]);
GUI.DrawTexture(Rect(Screen.width*0.04,Screen.height*0.85,100,100), numbers[digit2]);
GUI.DrawTexture(Rect(Screen.width*0.08,Screen.height*0.85,100,100), numbers[digit3]);
}
//Digit 4
//****************************************************************************************************************
if(s.Length==4){ // if s has 4 digits ****************************
GUI.DrawTexture(Rect(Screen.width*0.0,Screen.height*0.85,100,100), numbers[digit1]);
GUI.DrawTexture(Rect(Screen.width*0.04,Screen.height*0.85,100,100), numbers[digit2]);
GUI.DrawTexture(Rect(Screen.width*0.08,Screen.height*0.85,100,100), numbers[digit3]);
GUI.DrawTexture(Rect(Screen.width*0.12,Screen.height*0.85,100,100), numbers[digit4]);
}
}
I re wrote the whole code i think this is much better than what i had previously.
Answer by Bunny83 · Feb 06, 2014 at 10:54 AM
There's a much easier solution. Just use your original code and change line #23 to:
var s:String = TotalPoints.ToString("D4");
O$$anonymous$$ thank you!, but how can I deactivate the unused zeros ....???this just flips the order from right to left