Sprites as numbers, automatic.

Hello,

First of all sorry for the title, I really couldn’t came up with a good title xD

I have made a point system, included with GUI.Label. I don’t like the look of those numbers in game.
So I want to make it that if I have ‘‘1’’ point, there will come up a sprite with the number 1, which looks better.
I know how to make it because I already made it with my countdown system aswell.
In my countdown system it looked like this:

    float timeRemaining = 4.0f;

    void Update () {
        timeRemaining -= Time.deltaTime;
    }
  
    void OnGUI(){

        if (timeRemaining > 2 && timeRemaining < 3)
            count3.SetActive (true);          
        else
            count3.SetActive(false);

        if(timeRemaining > 0 && timeRemaining < 1)
            count1.SetActive(true);
        else
            count1.SetActive(false);

        if(timeRemaining > 1 && timeRemaining < 2)
            count2.SetActive(true);
        else
            count2.SetActive(false);

        if(timeRemaining > 0){
            GUI.Label(new Rect(600, 600, 1000, 600),
                      "Time Remaining : "+timeRemaining);

        }
        else{
            GUI.Label(new Rect(600, 600, 1000, 600), "Time's Up");
            Application.LoadLevel ("InGame");
        }
    }
}

(count1,2,3 are sprites)

But if I have a point system, the player can reach points like 27, 99, 375, 214, 69 and more…
Its isnt really nice to write that all out and use tons of sprites (for every number).
Instead of that I want to only have the numbers 0,1,2,3,4,5,6,7,8,9 and if I reach number 12, as example, there will come number 12 automaticly (with the sprites 1 and 2). And if I have number 235… yeah you understand it…

Is this possible, or is there a better way to do this?

Thank you.

You can try this:
2324281--156835--2015-10-04_10-32-16.gif

public class NewBehaviourScript : MonoBehaviour
{
    public float timeRemaining = 106.0f;
    public int third = 0;
    public int second = 0;
    public int first = 0;

    public GameObject[] FirstSprites = new GameObject[10];
    public GameObject[] SecondSprites = new GameObject[10];
    public GameObject[] ThirdSprites = new GameObject[10];

    void Start()
    {
        ActivateSprites(timeRemaining);
    }

    void Update()
    {
        timeRemaining -= Time.deltaTime;
        ActivateSprites(timeRemaining);
    }

    void ActivateSprites(float time)
    {
        // for example 159
        // tempValue = 9
        int tempValue = (int)time % 10;
        if (first != tempValue)
        {
            //            FirstSprites[first].SetActive(false);
            //            FirstSprites[tempValue].SetActive(true);
            Debug.Log("First. Deactivate : " + first);
            Debug.Log("First. Activate : " + tempValue);
            first = tempValue;
        }

        // tempValue = 5
        tempValue = (int)time / 10 % 10;
        if (second != tempValue)
        {
            //            SecondSprites[second].SetActive(false);
            //            SecondSprites[tempValue].SetActive(true);
            Debug.Log("Second. Deactivate : " + second);
            Debug.Log("Second. Activate : " + tempValue);
            second = tempValue;
        }

        // tempValue = 1
        tempValue = (int)time / 100;
        if (third != tempValue)
        {
            //            ThirdSprites[third].SetActive(false);
            //            ThirdSprites[tempValue].SetActive(true);
            Debug.Log("Third. Deactivate : " + third);
            Debug.Log("Third. Activate : " + tempValue);
            third = tempValue;
        }
    }
}

Thank you, I will try this out. It looks complicated ^^

I just a basic idea and u can optimize this, for example:

    // Sprites from assets
    public Sprite[] NumbersSprites = new Sprite[10];

    // Or SpriteRenderer
    public Image FirstNumber;
    public Image SecondNumber;
    public Image ThirdNumber;

    private void Update()
    {
        timeRemaining -= Time.deltaTime;
        FirstNumber.sprite = NumbersSprites[(int)timeRemaining % 10];
        SecondNumber.sprite = NumbersSprites[(int)timeRemaining / 10 % 10];
        ThirdNumber.sprite = NumbersSprites[(int)timeRemaining / 100];
    }

2324345--156842--2015-10-04_12-23-15.gif

So the 2 messages you send are 2 different scripts or…??
Unity doesn’t know ‘‘Image’’.

What does this do?

  • public Image FirstNumber;
  • public Image SecondNumber;
  • public Image ThirdNumber;

Please excuse me hijacking this thread - but what tool do you use to make these gifs?

You need add “using UnityEngine.UI;”.
Image - https://unity3d.com/ru/learn/tutorials/modules/beginner/ui/ui-image
FirstNumber and other is you UI sprites:

https://getsharex.com/

1 Like

Thankyou, it works perfectly :smile:

One question:

Is there a way to hide the ‘‘0’s’’ when they aren’t used.
So if I have the letter 5; I don’t see 005, but just only 5.

Thanks.

You need validate current numbers. For example(i dont test this in unity, just typed here):

    private void Update()
    {
        timeRemaining -= Time.deltaTime;
        int tempValue = (int)timeRemaining / 100;
        if (tempValue == 0)
            ThirdNumber.sprite = NumbersSprites[tempValue];
        else
            // Hide number
            ThirdNumber.sprite = null;

        int tempValue_2 = (int)timeRemaining / 10 % 10;
        if (tempValue != 0 && tempValue_2 != 0)
            SecondNumber.sprite = NumbersSprites[tempValue_2];
        else
            // Hide number
            SecondNumber.sprite = null;

        tempValue = (int)timeRemaining % 10;
        FirstNumber.sprite = NumbersSprites[tempValue];
    }

My code now:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PointSystem : MonoBehaviour {

    public float pointText;

    public Sprite[] NumbersSprites = new Sprite[10];

    public Image FirstNumber;
    public Image SecondNumber;
    public Image ThirdNumber;

    void Start ()
    {
        pointText = 0f;
    }
  

    private void Update ()
    {
        FirstNumber.sprite = NumbersSprites[(int)pointText % 10];
        SecondNumber.sprite = NumbersSprites[(int)pointText / 10 % 10];
        ThirdNumber.sprite = NumbersSprites[(int)pointText / 100];
  
        int tempValue = (int)pointText / 100;
        if (tempValue == 0)
            ThirdNumber.sprite = NumbersSprites[tempValue];
        else
            // Hide number
            ThirdNumber.sprite = null;
      
        int tempValue_2 = (int)pointText / 10 % 10;
        if (tempValue != 0 && tempValue_2 != 0)
            SecondNumber.sprite = NumbersSprites[tempValue_2];
        else
            // Hide number
            SecondNumber.sprite = null;
      
        tempValue = (int)pointText % 10;
        FirstNumber.sprite = NumbersSprites[tempValue];
    }


    void OnTriggerEnter2D (Collider2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            pointText += 1;
            Debug.Log ("The score should update +1");
        }
    }


    void OnGUI ()
    {
        GUI.Label(new Rect(-100, 10, 600, 900),
                  "Score : "+pointText);              
    }
}

The code you send disables the source image but than it doesn’t disable the color, so the whole image has to be disabled.

http://i.imgur.com/PNRGylL.gifv

btw. I use the same software as you use to make a gif, but how do I paste it into a thread like you?

So Image.sprite = null does work. You need then try this:

ThirdNumber.enable = false;
//or
ThirdNumber.gameObject.SetActive(false);

Also you need extra check:

        int tempValue = (int)pointText / 100;
        if (tempValue == 0)
        {
             if (!ThirdNumber.enable) ThirdNumber.enable = true;
            ThirdNumber.sprite = NumbersSprites[tempValue];
}
        else
            // Hide number
            ThirdNumber.enable = false;

To past gif use “Upload a File”.

only the second number doesn’t appear, but it also doesn’t appear if I reach 10+.

public class PointSystem : MonoBehaviour {

    public float pointText;

    public Sprite[] NumbersSprites = new Sprite[10];

    public Image FirstNumber;
    public Image SecondNumber;
    public Image ThirdNumber;

    void Start ()
    {
        pointText = 0f;
    }


    private void Update ()
    {
        FirstNumber.sprite = NumbersSprites[(int)pointText % 10];
        SecondNumber.sprite = NumbersSprites[(int)pointText / 10 % 10];
        ThirdNumber.sprite = NumbersSprites[(int)pointText / 100];

        int tempValue = (int)pointText / 100;
        if (tempValue == 0)
            ThirdNumber.sprite = NumbersSprites[tempValue];
        else
            // Hide number
            ThirdNumber.gameObject.SetActive(false);
    
        int tempValue_2 = (int)pointText / 10 % 10;
        if (tempValue != 0 && tempValue_2 != 0)
            SecondNumber.sprite = NumbersSprites[tempValue_2];
        else
            // Hide number
            SecondNumber.gameObject.SetActive(false);
    
        tempValue = (int)pointText % 10;
        FirstNumber.sprite = NumbersSprites[tempValue];
    }


    void OnTriggerEnter2D (Collider2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            pointText += 1;
            Debug.Log ("The score should update +1");
        }
    }


    void OnGUI ()
    {
        GUI.Label(new Rect(-100, 10, 600, 900),
                  "Score : "+pointText);            
    }
}

I also can’t use ‘‘enable’’ (also in the extra check there is ‘‘enable’’)

2326926--157071--2015-10-06_09-53-02.png
click^^

You are deactivate gameobject, and never activate it back. I have already said that you need additional check.

but as I said:

The script doesn’t work like this.

An answer already?

Instead of enable you need use SetActive.

I can’t use this:

if (!ThirdNumber.SetActive) ThirdNumber.SetActive(true);

Try GameObject.activeSelf - http://docs.unity3d.com/ScriptReference/GameObject-activeSelf.html

2338897--158117--3cf061da07b4820f17a057191fb9b110.png

First get gameObject from Image and then use activeSelf.

img.gameObject.activeSelf