Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I use sprites to display score in a 2D game?

Discussion in 'Scripting' started by emonegarand, Jan 13, 2014.

  1. emonegarand

    emonegarand

    Joined:
    Dec 17, 2013
    Posts:
    6
    I have a set of 7 sprites to display score within my game but I don't know the proper way to script them to animate to change each frame to the corresponding number.

    I have created 7 seperate GameObjects labeled One, Ten, Hundred, Thousand, TenThousand, HundredThousand and Million.



    So far I've thought that I could have an int for each place number and have them set back to zero upon hitting a value >= 10 and increment the next place number but that sounds sloppy, is there a better way to do this and how would I call for the game objects to change to the corresponding frame from a script? All the examples i've found for score keeping have only touched on using GUIText, while much easier it doesn't have the look I want.

    Can it be done with defining a game object within the script to point to a script using:

    Code (csharp):
    1. public GameController gameController;
    2.  
    3.     // Use this for initialization
    4.     void Start ()
    5.     {
    6.        GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
    7.        if (gameControllerObject != null)
    8.        {
    9.          gameController = gameControllerObject.GetComponent <GameController>();
    10.        }
    11.        if (gameController == null)
    12.        {
    13.          Debug.Log ("Cannot find 'GameController' script");
    14.        }
    15.     }
    Would I be calling that in a script that checks for the Animation State of the score object?

    I'm not sure where to start and my searches haven't gotten me anywhere.
     
  2. scryedzxp

    scryedzxp

    Joined:
    May 14, 2013
    Posts:
    47
    do this to get each of your digits:

    http://stackoverflow.com/questions/...-without-converting-it-to-a-string-char-array

    (where n would be your score) and then change the corresponding frame to that number.

    As for animating the frames (assuming that your 7 separate gameobjects are guitextures or quads with textures), there are several ways you can do it :
    1) you can have a texture for each digit (0-9) and then when you're ready to update a frame, you just simply switch that frame's texture to that digit texture
    2) you have have all your digits textures combined into 1 texture and arranged in a grid-like manner (this is called a texture atlas), and when you need the frame for a specific digit, you take that subsection of the texture (where the digit is) and then copy it over to your frame's texture

    both of these you'll have to code from scratch. first option is the easiest to code except that you'll have 10 small textures. second option is the hardest to code but you'll have 1 large texture to deal with.

    Not sure about your last two questions.
     
    Last edited: Jan 13, 2014
  3. emonegarand

    emonegarand

    Joined:
    Dec 17, 2013
    Posts:
    6
    I sorta grasp what this is trying to do but I don't have a clue how to implement it. I've seen people use graphics for counters in there GUI's that were not font based so I'm surprised I'm having such a hard time finding any examples on the web myself.
     
  4. rosdi

    rosdi

    Joined:
    Sep 15, 2012
    Posts:
    18
  5. Afassolas

    Afassolas

    Joined:
    Apr 10, 2013
    Posts:
    78