I wrote a script for distance of car and I don`t know how to make distance like this Distance: 156Km. I wrote this code:

distanceText.text = ((int)distance).ToString() + "Km";

It works only from 1 to 9; when it goes to 10 Km, it disappears but if I write it like this:

distanceText.text = ((int)distance).ToString() + " Km";

It show like this: from 1 to 9 -> "5 Km" and from 10 to 99 -> "99Km" but when it goes to 100 Km and above, it disappears. What code should I write for this to show always Km after the distance.

And I have one more question. I have a gameover canvas and I'd like in gameover canvas to show the final distance I make with PlayerPrefs but it doesn't work.

enter image description here

Here it shows how it does not work:

Dont work like this

  • \$\begingroup\$ Please keep your question to one question. For the first question, could you provide screenshots? I would also recommend using String.Format("%d Km", distance); \$\endgroup\$ – Cedric Martens Jun 30 '17 at 17:09
  • \$\begingroup\$ if you like i can upload all code. \$\endgroup\$ – Driton Jun 30 '17 at 18:06
  • \$\begingroup\$ I think I understand your problem, do you have one label saying "Distance : " and another seperate label saying "X Km"? \$\endgroup\$ – Cedric Martens Jun 30 '17 at 18:33
  • \$\begingroup\$ Yes but if i make like this doesent work distanceText.text = ((int)distance).ToString() + "Km"; \$\endgroup\$ – Driton Jun 30 '17 at 18:41

Instead of having two labels. One for Distance : and one for X Km, you should only have one. Set the label like this :

distanceText.Text = String.Format("Distance : {0} Km",(int)distance);

The String.Format method allows you to easily format strings, the %d indicates you want it to be formated with an int. {0} is where the argument 0 (distance) will be replaced.

  • \$\begingroup\$ dont work it show like in picture Distance : %d Km \$\endgroup\$ – Driton Jun 30 '17 at 19:13
  • \$\begingroup\$ To comply with StackExchange, one question is one question. If you want people to answer the other question. Please do another question that details your problem so people can answer accordinly answer. \$\endgroup\$ – Cedric Martens Jun 30 '17 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.