- Home /
Basic Game Score Question.
Hi there!
I'm creating a game where the player collects items and each time the player collides with an item the score increases using these simple scripts.
var theScore : int =0;
function Update () {
guiText.text = "Golden Orbs: "+theScore;
}
...
and the script on the player:
function OnControllerColliderHit(theCollision : ControllerColliderHit) {
if(theCollision.gameObject.name == "orb") {
Destroy(theCollision.gameObject);
gameObject.Find("score").GetComponent(textScript).theScore+=1;
}
}
at the end of the level time the level moves to a menu screen. I'd like to display this score in a guitext, what code would help me display this score bearing in mind its in a new level.
Thanks!
Answer by Bob5602 · Jan 11, 2011 at 08:55 PM
Here is what I do :
Make a new, empty game object, something like ScoreKeeper is what I use. Then, put a static variable on there like currentScore. Then, whenever you update the score as normal, you update the ScoreKeeper object, currentScore variable. Put a "dontdestroyonload" within the scorekeeper and you can keep it between levels. At the start of the next gameplay level, you can just zero it out and start over if you want to, or keep adding more score on to what you already had.
Your answer
Follow this Question
Related Questions
Change the game window built 1 Answer
Why isn't the game build displaying like it does in the editor? 0 Answers
Scoring System 1 Answer
Save and Display Best Score 2 Answers
getting gui error you can only call functions from inside OnGui. 1 Answer