Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 echo55 · Jul 30, 2015 at 08:38 AM · highscore

Problem with save HighScore

Hey all. My english is bad i know so sorry for it.

with friend we making quiz - answer game and i have big problem with High Score. In our game every question is in single scene. When i push button with incorect answer score are reseting to zero and game go to main menu, but high score reset too.

Im lerning C# and i dont know too much but im trying so hard. I use new UI in Unity 3D 4.6.

Sorry for long post i dont expect finished scripts but give me some hints :)

Here are my scripts

1) its script with scores:

 using UnityEngine;
 using UnityEngine.UI; // This is the call for the new UI, this MUST be in here for this to work.
 using System.Collections;
 
 public class ScoreManager : MonoBehaviour
 {
     public static int score;        // The player's score.
           
        Text Pytania;                      // Reference to the Text component.
         
     void Start ()
     {
         // Set up the reference.
         Pytania = GetComponent <Text> ();    
     }
             
     void Update ()
     {
         // Set the displayed text to be the word "Score" followed by the score value.
         Pytania.text = "Score: " + score; 

         }


2) Script for making scores whene u push button and load next question:

 using UnityEngine;
 using System.Collections;
 
 public class Punkty : MonoBehaviour {
       int score = 1;
 
 
 
     public void ZmianaLevelu (int zmienLevel){
         
         Application.LoadLevel (zmienLevel);
         
     }
     
     public void DodawaniePunktow (int DodajPunkty){
         
         
         ScoreManager.score += score;
         
         
     }
 }


3) script to reset score whene u chose incorect anwser and bring u bak to main menu:

 using UnityEngine;
 using System.Collections;
 
 public class reset : MonoBehaviour {
 
     public int dupa = 0;
 
 
     
     public void ZresetowaniePunktow (int resety){
         
         Application.LoadLevel (resety);
         ScoreManager.score = dupa;
 
 
     }
 
 }

4) its script for High Scores:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class BestScore : MonoBehaviour {
     
     public static Text player;
     int score = 0;
 
 
 
     void Update () {
         int l = ScoreManager.score;
         
         
         if(l > score){
             if(PlayerPrefs.HasKey("Best")){
                 score = PlayerPrefs.GetInt("Best");
             }
             PlayerPrefs.SetInt("Best", l);
             PlayerPrefs.Save();    
         }       
     
         GetComponent<Text> ().text = "BEST SCORE:  " + score;            
     }  
 
 
 }




Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NeverHopeless · Jul 30, 2015 at 09:00 AM

Try like this:

  void Update () {
      int l = ScoreManager.score;
      
      
      if(l > 0){

          if(PlayerPrefs.HasKey("Best")){
              score = PlayerPrefs.GetInt("Best");
          }

          if( l > score ) {  // Update score to PlayerPrefs iff this the best one
              PlayerPrefs.SetInt("Best", l);
              PlayerPrefs.Save();    
          }
      }       
  
      GetComponent<Text> ().text = "BEST SCORE:  " + score;            
  }  

Also, you should not use Update event to keep updating UI elements and display score/best score. Instead create functions for each task and either use:

 yourGameObject.GetComponent<YourScript>().CalculateBestScore();

or

 yourGameObject.SendMessage("CalculateBestScore", specifyReceiverHere)

Also, try avoiding things like this:

 Application.LoadLevel (resety);
 ScoreManager.score = dupa;


that is first navigate to some other screen and then take some action related to current screen. Such things produces crashes sometimes. So try to finish your current action then go to some other screen. like:

 ScoreManager.score = dupa;
 Application.LoadLevel (resety);


Hope it helps!

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 echo55 · Jul 30, 2015 at 08:09 PM 0
Share

its work !!! Thx mate i hope this post will help many people :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a PlayerPrefs Highscore 0 Answers

highscore/display system for multiple levels 2 Answers

Highscore with points and time 1 Answer

Add points to a lifetime score, once. 1 Answer

HighScore Names 1 Answer


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