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 Lonpot · Apr 05, 2014 at 05:00 PM · spritescoreguitext

Use sprites as score/GUItext?

Hello. I have a sprite for each number 0-9. How would i use them to display the players current score?

Do i have to create an animation and then specify what frame to be on from the code? If so do i then have to pre-make every score from 0-whateever? Or maybe dynamically change the sprite of an gameObject?

Are there any better way to approach this?

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 getyour411 · May 18, 2014 at 04:29 PM 0
Share

You'll have to keep a numeric score somewhere and then parse it to know which sprite to display in each position. Why not just use that numeric value in GUIText with font properties tweaked to your liking?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rosdi · May 18, 2014 at 04:43 PM

This is probably a late answer, but anyway here is how I do it. You need to have your score sprites named "0", "1", ... "9" in the game somewhere outside of the camera view otherwise this code will not work.

This code can be improved further for better performance.. but I for my game it is unnecessary since I don't have too many objects on the screen.

 public class ScoreManager : MonoBehaviour {
 
         private const float SPRITE_WIDTH = 0.07f;
         private const string SPRITE_TAG = "SCORE_SPRITE";   //http://answers.unity3d.com/questions/33597/is-it-possible-to-create-a-tag-programmatically.html
 
         void Start() {
 
         }
     
         public void UpdateScore (int score) {
 
             //remove previous scores
             GameObject[] oldScoreDigits = GameObject.FindGameObjectsWithTag(SPRITE_TAG);
             foreach (var oldScoreDigit in oldScoreDigits) {
                 Destroy(oldScoreDigit);
             }
 
             string scoreStr = score.ToString();
 
             for (int i = 0; i < scoreStr.Length; i++)
             {
                 GameObject digit = (GameObject)Instantiate(GameObject.Find(scoreStr.Substring(i,1)));
 
                 digit.tag = SPRITE_TAG;   //tag this object for easy removal
                 digit.transform.parent = this.transform;
                 digit.transform.localPosition = new Vector3(i * 0.08f, 0, transform.position.z);                
             }
             
             //center score to top screen
             Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width/2f, Screen.height * 0.96f, Camera.main.nearClipPlane));
             pos.x -= scoreStr.Length*SPRITE_WIDTH/2f; 
             transform.position = pos;
         }
     }


Put this script on a GameObject under your main camera. That way the score is always visible on the screen. As shown below.. this script is attached to Score game object under main camera.

alt text


score.jpg (32.2 kB)
Comment
Add comment · Show 1 · 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 Afassolas · Sep 25, 2014 at 10:51 AM 0
Share

it the code you state SPRITE_WIDTH = 0.07f;

i got sprites with width size 105 pixels how is that as float ? And I am having problem on the way to use this to pass the score so it will "print" on screen.

any suggestions?

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

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Score going up every second 1 Answer

What is the best way to show (Instantiate) a GUIText at the position of a GameObject? 3 Answers

Show score when enemy is destroyed C# 1 Answer

I used a GUIText to show the score in my game but it doesn't show my true results 0 Answers

Picking up an Item and telling you how many you have 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