- Home /
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;
}
}
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!
its work !!! Thx mate i hope this post will help many people :)
Your answer
Follow this Question
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