- Home /
GUIText not updating score....
So I got this guitext that holds my score, and depending on the distance that the player has traveled before he (inevitably) dies, the score updates. But for some reason, when i press the lovable play button, The score GUI is there, but it doesn't update. Not only that, its weird because what is displayed is the final score from the last time the game is played. For example, if you play one game, and die at at 254 meters, when you press "Try Again" (this reloads the scene through Application.LoadLevel), it starts again with the score GUI saying "Score: 254". From then on it doesn't update.
My code to hold the score is below:
using UnityEngine;
using System.Collections;
public class ScoreScript : MonoBehaviour {
//Scores
private double startPos = 0;
private double currentPos;
private int score;
//Score Stuffs
public GUIText CurrentScore;
// Use this for initialization
void Start () {
score = 0;
Instantiate(CurrentScore, new Vector3 (0.35f, 1f, 0f), Quaternion.identity);
}
// Update is called once per frame
void Update () {
currentPos = (int)(transform.position.x);
score = currentPos;
print (""+score);
CurrentScore.text = "Score: " + score.ToString();
}
}
Answer by Kiwasi · May 19, 2014 at 12:28 AM
You're doing some really weird stuff here. But I can solve your problem for you.
Put simply you instantiate your CurrentScore once in the start function. Instantiate creates a new copy of your object. Then in the update function you update CurrentScore.
Here is what is happening
Start() runs. Unity creates a CurrentScore(clone) object. "Score: 0"
Update() runs. CurrentScore is updated. CurrentScore(clone) is not
User reloads the level. CurentScore(clone) is deleted on level load. (Something outside of your code is also preventing the GameObject containing ScoreScript from being destroyed.)
Start() runs. A new CurrentScore(clone) is created from the present value of CurrentScore. "Score: 254"
Update() runs. CurrentScore is update. CurrentScore(clone) is not.
There are multiple ways to solve this problem. First you could assign CurrentScore to a GUIText you have created in the scene viewer. Remove line 19 completely. I have always had trouble working with GUIText directly and would nor recommend this method
Or you could use unity's built in OnGUI function. Code as follows:
using UnityEngine;
using System.Collections;
public class ScoreScript : MonoBehaviour {
//Scores
// By default unity uses floats instead of doubles
// The other variable aren't actually needed in this version of the script
private int score;
//Score Stuffs
private string scoreText;
// Use this for initialization
void Start () {
//Updating scoreText here as well as in update removes the carryover when loading levels
score = 0;
scoreText = "Score: " + score.ToString();
}
// Update is called once per frame
void Update () {
score = (int)(transform.position.x);
scoreText = "Score: " + score.ToString();
}
void OnGUI (){
GUI.Label (new Rect(20,20,100,100), scoreText);
//Alternatively use
GUILayout.Label(scoreText);
}
}
Sorry I didn't accept your answer for so long. I've avoided using the default unity GUI systems because they don't look good, but I recently discovered how to use GUI Skins, so they're fine with me now ;)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
multiply Score on Collision not working? check my code plz 2 Answers
Distribute terrain in zones 3 Answers