- Home /
Counting with GUI Textures
I made textures for the numbers 0-9 and im trying to count them up when i collect something.
My Code Is:
var g0 : Texture2D; var g1 : Texture2D; var g2 : Texture2D; var g3 : Texture2D; var g4 : Texture2D; var g5 : Texture2D; var g6 : Texture2D; var g7 : Texture2D; var g8 : Texture2D; var g9 : Texture2D;
var scoreOnes : int; var scoreTens : int;
var numberOnes; var numberTens;
//Gui Stuff var skin : GUISkin;
private var originalWidth = 1280.0; private var originalHeight = 800.0;
private var scale: Vector3;
function LateUpdate () {
Conversion();
}
function Update () {
//Adding To The Score
receiver = GameObject.FindWithTag("CollectionManager").GetComponent(CollectingFollowersReceiver);
scoreOnes = receiver.greenCnt + receiver.purpleCnt + receiver.blueCnt + receiver.yellowCnt + receiver.whiteCnt;
}
function OnGUI () {
scale.x = Screen.width/originalWidth;
scale.y = Screen.height/originalHeight;
scale.z = 1;
GUI.skin = skin;
var svMat = GUI.matrix;
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
GUI.Box(Rect(100,0,230,50), "");
GUI.DrawTexture(Rect(250,0,50,50), numberOnes);
GUI.DrawTexture(Rect(210, 0, 50, 50), numberTens);
GUI.matrix = svMat;
}
function Conversion () {
if(scoreOnes == 0) {
numberOnes = (g0);
}
if (scoreOnes == 1) {
numberOnes = (g1);
}
if (scoreOnes == 2) {
numberOnes = (g2);
}
if (scoreOnes == 3) {
numberOnes = (g3);
}
if (scoreOnes == 4) {
numberOnes = (g4);
}
if (scoreOnes == 5) {
numberOnes = (g5);
}
if (scoreOnes == 6) {
numberOnes = (g6);
}
if (scoreOnes == 7) {
numberOnes = (g7);
}
if (scoreOnes == 8) {
numberOnes = (g8);
}
if (scoreOnes == 9) {
numberOnes = (g9);
}
if (scoreOnes == 10) {
scoreOnes = 0;
numberOnes = (g0);
scoreTens = scoreTens + 10;
}
if (scoreTens == 0) {
numberTens = (g0);
}
if (scoreTens == 10) {
numberTens = (g1);
}
if (scoreTens == 20) {
numberTens = (g2);
}
if (scoreTens == 30) {
numberTens = (g3);
}
if (scoreTens == 40) {
numberTens = (g4);
}
if (scoreTens == 50) {
numberTens = (g5);
}
if (scoreTens == 60) {
numberTens = (g6);
}
if (scoreTens == 70) {
numberTens = (g7);
}
if (scoreTens == 80) {
numberTens = (g8);
}
if (scoreTens == 90) {
numberTens = (g9);
}
}
I know this is a little hard to understand but this posting crap is working against me. What im trying to do is change the GUI Textures based on the score variables, the thing is, When scoreOnes = 10, it resets the texture back to 0 and not the score itself. Then on top of that the Texture for the tens place goes to 1 but counts up to 9 rapidly because it in a function that is called everyframe.
Sorry for this mess of a post any help would be great.Thanks
Answer by aldonaletto · Oct 23, 2011 at 10:10 PM
A better solution would be to delete the variables g0 to g9 and use an array to store the textures - something like this:
var digits: Texture2D[]; // set size and elements at the Inspector
The only other modification would be to change the Convert function to this:
function Convert(){
numberOnes = digits[scoreOnes % 10]; // get the remainder of scoreOnes/10
numberTens = digits[scoreOnes / 10]; // get the integer result of scoreOnes/10
}
But if you don't want to waste the work spent in filling the elements g0 to g9, just modify your script like below:
var g0 : Texture2D; var g1 : Texture2D; var g2 : Texture2D; var g3 : Texture2D; var g4 : Texture2D; var g5 : Texture2D; var g6 : Texture2D; var g7 : Texture2D; var g8 : Texture2D; var g9 : Texture2D;
var scoreOnes : int;
var numberOnes: Texture2D; var numberTens: Texture2D;
//Gui Stuff var skin : GUISkin;
private var originalWidth = 1280.0; private var originalHeight = 800.0;
private var scale: Vector3;
function LateUpdate () { Conversion(); }
function Update () {
//Adding To The Score receiver = GameObject.FindWithTag("CollectionManager").GetComponent(CollectingFollowersReceiver); scoreOnes = receiver.greenCnt + receiver.purpleCnt + receiver.blueCnt + receiver.yellowCnt + receiver.whiteCnt; }
function OnGUI () {
scale.x = Screen.width/originalWidth;
scale.y = Screen.height/originalHeight;
scale.z = 1;
GUI.skin = skin;
var svMat = GUI.matrix;
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
GUI.Box(Rect(100,0,230,50), "");
GUI.DrawTexture(Rect(250,0,50,50), numberOnes);
GUI.DrawTexture(Rect(210, 0, 50, 50), numberTens);
GUI.matrix = svMat;
}
function Conversion () {
var ones: int = scoreOnes % 10; // get the ones digit
var tens: int = scoreOnes / 10; // get the tens digit
numberOnes = GetDigit(ones);
numberTens = GetDigit(tens);
}
function GetDigit (digit: int): Texture2D {
switch (digit){
case 0: return g0;
case 1: return g1;
case 2: return g2;
case 3: return g3;
case 4: return g4;
case 5: return g5;
case 6: return g6;
case 7: return g7;
case 8: return g8;
case 9: return g9;
}
} NOTE: This will not animate the digits, just display them.
Say if i wanted to add a hundreds place and a thousands place, Could i use: var hundreds: int = scoreOnes / 100; var thousands: int = scoreOnes / 1000;
To have more digits, you can do the following:
var ones: int = scoreOnes % 10; // get the rightmost digit
scoreOnes = scoreOnes / 10; // shift the value one digit to the right
var tens: int = scoreOnes % 10; // get the rightmost digit
scoreOnes = scoreOnes / 10; // shift one more digit to the right
var hundreds: int = scoreOnes % 10;
scoreOnes = scoreOnes / 10;
var thousands: int = scoreOnes % 10;
The idea is to get the rightmost digit with the operation % 10 (remainder of a division by 10), then shift the value one digit to the right with an integer division by 10 and get the rightmost digit with % 10, and so on. The most significant digit actually doesn't need the % 10 operation because it's already between 0 and 9, but you can keep the operation just for the case you need more digits in the future.
Your answer
Follow this Question
Related Questions
Show GUI texture on collison 1 Answer
script to create gui when detection collision between cube and first controler person ?? 1 Answer
Enable a GUI texture after player collides with another object 2 Answers
Question about pop up boxes during the game 0 Answers
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer