- Home /
Scoring Issue
I am making a simple platform game. The score system I implemented doesn't work the way I expected and I've been digging through similar questions and all over the internet but cannot resolve my issue. The problem I am having is that when my character jumps on a platform, he is to be awarded a point. I've set up a GameMaster script with a global variable to hold the score across all my objects, I've set up trigger boxes for my character to collide with using the OnTriggerEnter button. This works and updates a test variable in the game master script as it should, incrementing the score each collision. However.. whenever I play the game, my score doesn't change in real time.. after I cancel the game, the new score will update in the game scene but not while I am actually playing which.. isn't desired. Here is the code for this section of the game.
My score script attached to each platform trigger: var scoreText : GUIText; //applying 3d text from unity to here will allow manipulation //private var alreadyScored : boolean = false; //already scored on current platform
function Start () {
scoreText.text = "Score: 0"; //append score value to score text, applies to game scene object
}
function OnTriggerEnter(player : Collider) { //when something enters trigger space (checkpoint
if(player.tag == "Player" ) { //if that something is the player object
GameMaster.currentScore += 1;
scoreText.text = "Score: " + GameMaster.currentScore; //apply new score
Debug.Log("Scored a point!");
}
}
My game master script:
static var currentScore : int = 0; //global accessible script
var score = 0;
function Update () {
score = currentScore;
}
That looks like it should work, but I'd be tempted to handle the score stuff in the master script... simply use the collision to increment the score, keep the GUI object updating in realtime.
Thank you! I've transferred the score handling to the master script as suggested and moved the actually updating of the GUI object to the update method here, it works perfectly.
Here is the code for reference:
static var currentScore : int = 0; //global accessible script
var score = 0; //test score
var scoreText : GUIText; //applying 3d text from unity to here will allow manipulation
//private var alreadyScored : boolean = false; //already scored on current platform
function Start () {
scoreText.text = "Score: 0"; //starting text
}
function Update () {
score = currentScore; //test
scoreText.text = "Score: " + currentScore; //apply new score
}
function OnTriggerEnter(player : Collider) { //when something enters trigger space (checkpoint
if(player.tag == "Player" ) { //if player object collides
currentScore += 1; //increase score by 1
Debug.Log("Scored a point!");
}
}