- Home /
GUI Label Score Overlapping - Javascript
In my game, I have a score script which is attached to an object in my main game scene. I have it set so that when the player "dies" in the game it takes you to the game over screen and DOES NOT destroy the score so that you can see what your score was on game over. However, when the game restarts, the old score is still there and is overlapping over my brand new score. How can I fix this?
#pragma strict
var playerScore : int = 0;
//Style
var scoreStyle : GUIStyle;
function Start () {
playerScore = 0;
}
function Update () {
}
function OnGUI () {
if(Application.loadedLevel == 3) { //Game Level
GUI.Label (new Rect(20,20,10,10), playerScore.ToString(), scoreStyle);
}
if(Application.loadedLevel == 7) { //Game Over Screen
GUI.Label(Rect(Screen.width/2, Screen.height/2, 10, 10), playerScore.ToString(), scoreStyle);
}
}
Logically if you have two things in the same x,y that'll happen, so move one or the other somewhere x,y-else
...unless you are asking how do you get rid of the var on game restart?
I want to get rid of the score when the game restarts and create a new label that resets the score to 0
Bump. I'm still struggling to achieve my desired results. I +1 getyour411, and I appreciate the response, but his answer isn't working for me. Does anyone else know how to solve this issue? I should note that my button controls are separate from the "$$anonymous$$ain score" controls and the main score script is separate from all my other scripts. It's baffling me that I've struggled this much with such a see$$anonymous$$gly simple task
Answer by getyour411 · Apr 13, 2014 at 09:25 PM
Use OnLevelWasLoaded() to reset to 0
This succeeds in resetting my score, yes, but it still doesn't stop the GUI Labels from overlapping each other. What I want is when the player hits "main menu" or "replay" on the game over screen, the GUI Label is removed so that when a new game starts, there aren't 2 instances of the same object
So change that comment into a
if(thatCondition) {
// set a bool to true
}
then in your OnGUI wrap the display of the score you dont want like
if(!thatBool)
GUI.label...