- Home /
Show "Score:5" on my screen, how?
Hi...
I have this script
var score = 0;
function OnTriggerEnter( other : Collider ) { Debug.Log("OnTriggerEnter() was called"); if (other.tag == "Coin") { Debug.Log("Other object is a coin"); score += 5; Debug.Log("Score is now " + score); Destroy(other.gameObject); } }
function OnGUI () { GUI.Label (Rect (10, 10, 100, 20), score.ToString()); }
At now the script print on my screen "5" if i collect 1 coin. I want to print "Score:5"
Can you correct my script please? Thank you.
Answer by burnumd · Nov 22, 2010 at 05:05 PM
You can concatenate a string using the "+" operator. Whenever there's a string and you use +
, other arguments are automatically converted to string unless you put them in parentheses. So
"Score: " + 5; // returns the string "Score: 5"
and
"Score: " + (1 + 4); // returns the string "Score: 5"
So whenever you change your score, you can change the score string.
var score = 0; var scoreText = "Score: 0";
function OnTriggerEnter( other : Collider ) { Debug.Log("OnTriggerEnter() was called"); if (other.tag == "Coin") { Debug.Log("Other object is a coin"); score += 5; scoreText = "Score: " + score; Debug.Log("Score is now " + score); Destroy(other.gameObject); } }
function OnGUI () { GUI.Label (Rect (10, 10, 100, 20), scoreText); }
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Problem with the GUISkin on the script 1 Answer
Realistic Bouncing Effect 1 Answer
GUI button not showing up 1 Answer
Need help with my script 0 Answers