• 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
Question by swapras · Jun 17, 2015 at 07:21 PM · uitextcanvas

How to add total quantity of a score after the changing score Int?

Pardon me if I wasn't understandable with my question header, but I want to add "/12" after my score that I'm showing on the canvas.

Note that I was following the project "Roll a Ball" on the unity website and just wanted to make this small tweak where I show the total number of collectibles along with the currently collected. Something like "Collected: n/12" "n" being the current score.

I'm completely new hear, so pardon me again if I wasn't understandable. I lack the knowledge jargon too.

Here's the whole code where I've put the scoring text:

 using UnityEngine.UI;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public float speed;
     public Text countText;
     public Text winText;
     public Text escapeInfo;
 
     private Rigidbody rb;
     private int count;
     private string total;
 
     void Start ()
     {
         rb = GetComponent<Rigidbody>();
         count = 0;
         SetCountText ();
         winText.text = "";
         escapeInfo.text = "";
     }
 
     void Update ()
     {
         if (Input.GetKey (KeyCode.Escape))
             Application.Quit ();
     }
 
     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
     
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         
         rb.AddForce (movement * speed);
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Pick Up"))
         {
             other.gameObject.SetActive (false);
             count = count + 1;
             SetCountText ();
         }
     }
     void SetCountText ()
     {
         countText.text = "Collect: " + count.ToString ();
         if (count >= 12)
         {
             winText.text = "You Win";
             escapeInfo.text = "Press "Esc" to Exit";
         }
     }
 }
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Dave-Carlile · Jun 17, 2015 at 07:32 PM

On line 51:

 countText.text = "Collect: " + count.ToString () + "/12";


Or you can use string.format which might be a little more clear:

 countText.text = string.Format("Collect: {0}/12", count);
Comment
eldruz
imaxus

People who like this

2 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 swapras · Jun 18, 2015 at 07:12 AM 0
Share

Thanks for the answer but what exactly is string.Format and how does it work as opposed to what I did?

avatar image Maurice_B swapras · Jun 06, 2016 at 08:52 AM 0
Share

It is displaying:

  Collected: n/12

your code just says:

Collected: n

As far as my experience goes String.Format used in this way can put variables inside strings without concatenation (the {0} becomes the value of the first thing after the comma), Look it up on the docs if you want more info. Also mark Daves answer as the answer so the question is answered :D

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Missing canvas elements on Build & Run. 2 Answers

5.3.5 - Blurry UI text after switching canvasses - until text is refreshed by turning visibility off/on or changing resolution 4 Answers

Unity UI Text Blurriness 0 Answers

Multiple Text of canvas with One script ?? 1 Answer

How do I change scene with two ui canvas text 2 Answers


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