- Home /
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";
}
}
}
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);
Thanks for the answer but what exactly is string.Format and how does it work as opposed to what I did?
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
![](https://koobas.hobune.stream/wayback/20220613201108im_/https://answers.unity.com/themes/thub/images/avi.jpg)