- Home /
Score wont stop after game is paused
My score is time based. If i pause the game the score just continues adding. Anyone know how to fix?Heres my pause and score scripts. Score script
public int Score = 0;
public int highScore = 0;
public bool hit;
public GUISkin myskin;
void Start(){
highScore = PlayerPrefs.GetInt ("HighScore",0);
}
void Update(){
Score += (int)Time.time;
}
public void IncreaseScore(int amount)
{
Score += amount;
}
void OnDisable()
{
if (Score > highScore) {
//highScore = playerScore;
PlayerPrefs.SetInt ("HighScore", Score );
PlayerPrefs.Save();
}
PlayerPrefs.SetInt ("Score", (int)Score );
}
void OnGUI()
{
GUI.skin = myskin;
GUI.Label(new Rect(600,50,200,100), "Score:" + (int)(Score));
}
Pause script:
public GUISkin myskin; public GUISkin my1skin; public string levelToLoad; public bool paused = false;
private void Start()
{
Time.timeScale=1; //Set the timeScale back to 1 for Restart option to work
}
private void OnGUI()
{
GUI.skin = myskin;
if (GUI.Button(new Rect(700,10,50,50),"")) //check if Escape key/Back key is pressed
{
if (paused){
paused = false;
//unpause the game if already paused
}else{
paused = true;//pause the game if not paused
}
if(paused){
Time.timeScale = 0; //set the timeScale to 0 so that all the procedings are halted
}else{
Time.timeScale = 1; //set it back to 1 on unpausing the game
}
}
GUI.skin = my1skin;
if (paused){
GUI.Box(new Rect(Screen.width/4, Screen.height/4, Screen.width/2, Screen.height/2), "PAUSED");
//GUI.Label(new Rect(Screen.width/4+10, Screen.height/4+Screen.height/10+10, Screen.width/2-20, Screen.height/10), "YOUR SCORE: "+ ((int)score));
if (GUI.Button(new Rect(Screen.width/4+10, Screen.height/4+Screen.height/10+10, Screen.width/2-20, Screen.height/10), "RESUME")){
paused = false;
Time.timeScale = 1;
}
if (GUI.Button(new Rect(Screen.width/4+10, Screen.height/4+2*Screen.height/10+10, Screen.width/2-20, Screen.height/10), "RESTART")){
Application.LoadLevel(Application.loadedLevel);
}
if (GUI.Button(new Rect(Screen.width/4+10, Screen.height/4+3*Screen.height/10+10, Screen.width/2-20, Screen.height/10), "MAIN MENU")){
Application.LoadLevel("-main menu");
}
}
}
wow sorry. maybe you edited your question or just it doesn't load for me correctly. Now i don't see it :)
oh yeah sorry i edited it because i made a typo with the braces:p
Answer by MayhemMike · Apr 20, 2015 at 03:13 AM
I hope I didn't miss something but the problem is that Update() is unaffected by Time.timeScale = 0; Time.timeScale only affects frame rate independent functions.
Try using FixedUpdate() instead of Update() then it should work
Answer by hangemhigh · Apr 19, 2015 at 07:41 PM
Your problem lays here
void Update(){
Score += (int)Time.time;
}
It is running even when paused. You need to define a variable that tells it to increase only if game is not pause. For example,
private bool gameIsPaused =false;
void Update()
{
if(!gameIsPaused){
Score += (int)Time.time;
}
}
You can then have a public function you can call to tell it when game is paused or not
public void setGamePaused(bool isPaused){
gameIsPaused = isPaused;
}
This function can then be called from your pause menu with true, or resume menu with false.
EDIT: In your pause script, you can do this.
public class Example: MonoBehaviour {
PauseMenuScript myPauseMenuScript;
void Awake(){
//Get the PauseMenuScript attached to the gameobject called guy
myPauseMenuScript= GameObject.Find("guy").GetComponent<PauseMenuScript>();
}
private void OnGUI()
{
/*Inside your puase menu in your gui which you have up here, you can call the function from the other class like below*/
myPauseMenuScript.setGamePaused(true);
//Inside your unpause menu
myPauseMenuScript.setGamePaused(false);
}
}
sorry im new to unity. Could you tell me how i can call the function from my pause script?
Sure. What is the name of your pause script and what is the name of the GameObject your "GUISkin" is attached to in the unity editor?
The name of my pause script is Pause$$anonymous$$enuScript and the objects name is guy.
I updated my answer. Assu$$anonymous$$g that "guy" is gameobject created in the editor and "Pause$$anonymous$$enuScript" is the script name attached to the "guy" gameobject. It should work. If you are still having problems, just post the full two classes and I will modify them here.
Thank you so much this worked great but putting in the fixedUpdate was just easier
Answer by rockyourteeth · Apr 20, 2015 at 07:14 PM
You're adding the Time.time to your score every frame. What this means is that you'll start with 0, and at the first second, you'll add 1, then on the second second, you'll add 2 to your score, etc. Your score will keep getting incrementally larger.
I'd recommend something like this:
int score; float score_float;
Then, in your update function:
score_float += Time.deltaTime; score = (int)score_float;
"Time.deltaTime" will add to your score at a steady rate each frame, and it will also pause when the game clock is paused, but you'll have to keep your score stored as a float value so it can get small updates each frame. Then you can cast it as an int in order to have your main score variable.
You might actually also want to add a variable like this:
float scoreMultiplier = 1.0f;
And then multiply deltaTime by it:
score_float += Time.deltaTime * scoreMultiplier ;
This way, you can make your score count of faster or slower by changing your score multiplier.
Your answer
Follow this Question
Related Questions
Pause Time.time? 1 Answer
Time.timeScale seems doesn't work with Update 1 Answer
How do I do I time-base score script 0 Answers
Countdown timer using Time.unscaledDeltaTime not working as expected. 1 Answer
Get score in areas 0 Answers