How to implement an ammo counter using textured quads? [Scripting doubts]

I am trying to implement a counter who will show current ammo in an iphone game (similar to a gui item).

The approach i have been testing is by using 10 quads for each digit (0-9) who all share the same material/texture (to batch them into 1 draw call), the reason for not using a guitext or similar element is that i need a small animation on each of them, similar to a "pop-up" on each digit everytime the counter changes its value.

Example: Lets imagine the maximum ammo i can have at any given time is 99. For this i have a setup of 10 quads for the the first units digit and another 10 quads for the dozens digit. All of them have the renderer disabled, except for the ones who actually show the digits i want.

My doubt: I want to collect an integer variable that represents the current ammo (ie: 24) and separate it into "2" and "4", so that my quad group representing the dozens turns on the renderer showing "2" and the other group shows "4".

Anyone can point me in what to research, or any example of a correct approach? My current code test (limited by scripting knowledge) will soon become a mess Sad (imagine this for 100 cases lol)


//show "2" on dozens counter
counterA0.renderer.enabled = false;
counterA1.renderer.enabled = false;
counterA2.renderer.enabled = true; //quad showing "2" for dozens
counterA3.renderer.enabled = false;
counterA4.renderer.enabled = false;
counterA5.renderer.enabled = false;
counterA6.renderer.enabled = false;
counterA7.renderer.enabled = false;
counterA8.renderer.enabled = false;
counterA9.renderer.enabled = false;

//show "4" on units counter
counterB0.renderer.enabled = false;
counterB1.renderer.enabled = false;
counterB2.renderer.enabled = false;
counterB3.renderer.enabled = false;
counterB4.renderer.enabled = true; //quad showing "4" for units
counterB5.renderer.enabled = false;
counterB6.renderer.enabled = false;
counterB7.renderer.enabled = false;
counterB8.renderer.enabled = false;
counterB9.renderer.enabled = false;

Thanks in advance!

The solution I give is far from a good one, but it uses the counterA/B0-9 as you've defined and should be easy to understand (works from 0 .. 99 but it should be easy to add a digit).

var digitA = (ammo / 10) % 10;
var digitB = (ammo % 10);

switch (digitA) {
    case 0 : counterA0.renderer.enabled = false; break;
    case 1 : counterA1.renderer.enabled = false; break;
    case 2 : counterA2.renderer.enabled = false; break;
    case 3 : counterA3.renderer.enabled = false; break;
    case 4 : counterA4.renderer.enabled = false; break;
    case 5 : counterA5.renderer.enabled = false; break;
    case 6 : counterA6.renderer.enabled = false; break;
    case 7 : counterA7.renderer.enabled = false; break;
    case 8 : counterA8.renderer.enabled = false; break;
    case 9 : counterA9.renderer.enabled = false; break;

switch (digitB) {
    case 0 : counterB0.renderer.enabled = false; break;
    case 1 : counterB1.renderer.enabled = false; break;
    case 2 : counterB2.renderer.enabled = false; break;
    case 3 : counterB3.renderer.enabled = false; break;
    case 4 : counterB4.renderer.enabled = false; break;
    case 5 : counterB5.renderer.enabled = false; break;
    case 6 : counterB6.renderer.enabled = false; break;
    case 7 : counterB7.renderer.enabled = false; break;
    case 8 : counterB8.renderer.enabled = false; break;
    case 9 : counterB9.renderer.enabled = false; break;

As I said, this works, with the setup you have now, and maybe for now you should leave it by that until you've learned more about unity and scripting. To give you a lead to a better solution:

Instead to have for every possible value per digit another gameobject a simple optimization would be to have 10 different materials each with another digit and just change the material for the digit gameobject. If you put the 10 materials into an array you can just change the material using the calculated digit (digitA or digitB vars) as index instead of using those long switch statements.

what about changing the texture?

like from the docs:

var someTexture : Texture2D; guiTexture.texture = someTexture;

so, define 10 texture for each digit and swap the texture of the tenth and the, well other digit dont know how its called in english ;-)

greetings, thomas