- Home /
2 int's displayed in one GUIText.
Okay, I have this
guiText.text = "" +displayMinutes ":" +displaySeconds;
attached to a GUIText object. Unfortunately it doesn't work...
guiText.text = "" +displayMinutes;
works perfectly, as does
guiText.text = ":" +displaySeconds;
I just can't put them together.
Obviously the rest of this script is a timer script, which works correctly. So, what do I do...
Answer by almo · May 13, 2011 at 05:22 PM
Did you mean
guiText.text = "" + displayMinutes + ":" + displaySeconds;
Though I would recommend
guiText.text = displayMinutes.ToString() + ":" + displaySeconds.ToString();
for clarity.
Thank you, that did it. Sorry for such a dumb question, I am new to this...
Does ToString() mess up the number properties of the variable, or does it not permanently change it to a string?
ToString is a function that returns a string containing a representation of the object it is called on. If you search Unity's scripting docs for ToString, you will see a lot of different objects support it. So it won't alter the object you call it on. :)
"adding" something to a string will automatically ToString-ify it, so putting the .ToString() call is optional, but perhaps makes it more clear what's happening.
ToString() just makes it more confusing to me, and takes longer to type, so I will carry on with what I am doing. Anyway, thanks for the clarification guys...
Answer by yoyo · May 13, 2011 at 06:58 PM
Adding strings together with + works fine. Another option though is to use string formatting, like so ...
guiText.text = string.Format("{0}:{1}", displayMinutes, displaySeconds);
Whether you like the syntax better is a matter of taste, but this also gives you much more control over how things are formatted. See the documentation for String.Format for details.
Answer by FLASHDENMARK · May 13, 2011 at 05:33 PM
Every time you want to add something to a guitext then you will need to have +s in between.
When I was first learning I used to memorized by remembering how I write text:
Lets say I want to add several things to a guiText I used to compare it to a grocery list e.g:
My shopping list!
I need: Milk, bread, dogfood, 20 tampons, candy, batteries. And so on. If I wanted to use that in a guiText then i would say:
guiText.text = "" + Milk + dogfood + 20 tampons + candy + ":" + batteries;
If you get my point I just replaced the commas with +s.
I thought I would tell you this because it helped me a lot when I was first learning.
Your answer
Follow this Question
Related Questions
GUI Text to GUI Label Script? 1 Answer
How do I change the text of a gui image text 1 Answer
Animation depending on the Int value 0 Answers
changing font + size of text 1 Answer
Random int Value 3 Answers