- Home /
Why my score system not working?
Hi, can someone answer me, why this is not working for me (it's score system):
var score1 : GUIText;
var myScore : int = 0;
function Update () {
score1.text = "Score: " + myScore;
}
function OnTriggerEnter (col : Collider) {
if(col.gameObject.tag == "Player") {
myScore += 10;
}
Destroy(this.gameObject);
}
Does this go on a pickup? As Aldonalto points out, it kills itself when you hit it, which is what a pickup should do. But it also holds the main score variable (myScore?) which is what the player should be doing.
Good point! I thought this was a kind of checkpoint - if it's a pickup item, the myScore variable must be kept in another script - maybe in the GUIText script, for instance. Another simple yet effective alternative is to declare myScore as static in this code:
static var myScore : int = 0;
Even being declared in every instance of the script above, a static variable is created and initialized only once, and survive till the end of the game.
Answer by aldonaletto · May 26, 2013 at 03:14 PM
You're destroying the trigger when the player enters it! Remove line 25 (Destroy(this.gameObject);) and your code should work.
Your answer
Follow this Question
Related Questions
I want my score to reset back to 0 but keep my highscore saved 3 Answers
Score Multiplier by Time 1 Answer
3 star time base system 0 Answers