Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DAlturG · Apr 18, 2015 at 11:11 PM · timerscorepausetime.timescale

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");
             } 
         }
     }

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hbalint1 · Apr 19, 2015 at 12:58 PM 0
Share

in the OnGUI() method: }{ there are some wrong braces.

avatar image DAlturG · Apr 19, 2015 at 07:21 PM 0
Share

could you tell me where the wrong braces are?

avatar image hbalint1 · Apr 19, 2015 at 07:31 PM 0
Share

wow sorry. maybe you edited your question or just it doesn't load for me correctly. Now i don't see it :)

avatar image DAlturG · Apr 19, 2015 at 07:34 PM 0
Share

oh yeah sorry i edited it because i made a typo with the braces:p

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DAlturG · Apr 20, 2015 at 06:27 PM 0
Share

Thanks this worked

avatar image
1

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);
         }
      }











Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DAlturG · Apr 19, 2015 at 08:12 PM 0
Share

sorry im new to unity. Could you tell me how i can call the function from my pause script?

avatar image hangemhigh · Apr 19, 2015 at 08:23 PM 0
Share

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?

avatar image DAlturG · Apr 19, 2015 at 08:26 PM 0
Share

The name of my pause script is Pause$$anonymous$$enuScript and the objects name is guy.

avatar image hangemhigh · Apr 19, 2015 at 08:57 PM 0
Share

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.

avatar image DAlturG · Apr 20, 2015 at 06:25 PM 0
Share

Thank you so much this worked great but putting in the fixedUpdate was just easier

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges