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.
burnumd
2
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);
}