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
0
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
Add comment
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
2

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
Add comment · 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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Canvas is flipping, 2D game 0 Answers

,InputField backspace returns already send text 0 Answers

Unity 5 UI GUI Text Canvas Screen Resolution Adaption Problem 2 Answers

Access and modify Text within a prefab 1 Answer

How to know if your Text element will create a new line? 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