Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by TripodGRANNE · Oct 23, 2011 at 09:35 PM · collisionguiguitexturereset

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Oct 23, 2011 at 09:56 PM 0
Share

That would be 100X simpler if you used an array.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TripodGRANNE · Oct 24, 2011 at 12:06 AM 0
Share

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;

avatar image aldonaletto · Oct 24, 2011 at 03:44 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges