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 Ryujose · Aug 24, 2014 at 05:22 AM · playerprefssavescorehighscores

PlayerPrefs HighScore problems, it doesn't work.

Hello community, I want to save highscore when player go to mainscreen or the scene gameOver. But I don't know how to do it and I'm trying to at least save the score but it doesn't Works.

Here's my complete code.

 using UnityEngine;
 using System.Collections;
 
 public class ScoreScript : MonoBehaviour {
 
 
     // Interfaz Grafica Script
 
     public GUIText scoreText;
     public GUIText highScoreText;
     private int score;
     //private int newScore;
     private int highScore;
     public float widthScore = 0.0f;
     public float heightScore = 0.0f;
 
     
     void Start () 
     {
         score = 0;
         UpdateScore ();
     }
     
     public void AddScore (int newScoreValue)
         
     {
         score += newScoreValue;
         UpdateScore ();
     }
     
     
     
     void UpdateScore ()
     {
         scoreText.text = "Score: " + score;
         highScoreText.text = "Highscore: " + highScore;
         scoreText.pixelOffset = (new Vector2 (Screen.width / widthScore, Screen.height / heightScore));
     }
     public void StoreHighscore(int newHighscore)
     {
         int oldHighscore = PlayerPrefs.GetInt("highscore", 0);  
         if(newHighscore > oldHighscore)
             PlayerPrefs.SetInt("highscore", newHighscore);
         highScore = newHighscore;
         UpdateScore ();
     }
 
 }

Any help will be apreciate Im turning crazy...

Comment
Add comment · Show 3
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 zharik86 · Aug 24, 2014 at 07:06 AM 0
Share

Where call you function StoreHighscore()? $$anonymous$$aybe, call this function when you script loaded new level?

avatar image Ryujose · Aug 24, 2014 at 02:18 PM 0
Share

I called here but it doesn't work. I've called on boss dead.

 using UnityEngine;
 using System.Collections;
 
 /// Comportamiento de la salud de los objetos
 
 public class SaludBossScript : $$anonymous$$onoBehaviour {
     
     
     /// Numero de puntos de salud
     
     public int ps = 300;
     
     private GameObject Boss;
     private ScoreScript scoreScript;
     public int ScoreValue;
     private int HighScoreValue;
 
 
     /// Es jugador o enemigo?
     
     public bool esEnemigo = true;
     
     // LLamamos el script score y el tag enemigo1
     
     void Start ()
     {
         GameObject scoreScriptObject = GameObject.FindGameObjectWithTag ("ScoreScript");
         if (scoreScriptObject != null) {
             scoreScript = scoreScriptObject.GetComponent <ScoreScript> ();
         }
         if (scoreScript == null) {
             Debug.Log ("No se puede encontrar ScoreScript");
         }
         
         Boss = GameObject.FindWithTag ("Boss");
         if (Boss == null) {
             Debug.Log ("No se encuentra a Boss");
         }
     }
     
     
     
     void OnTriggerEnter2D(Collider2D collider)
     {
         //Es un disparo?
         Disparar disparo = collider.gameObject.GetComponent<Disparar>();
         if (disparo != null)
         {
             // Revisamos si es enemigo o compañero
             if (disparo.esDisparoEnemigo != esEnemigo)
             {
                 ps -= disparo.daño;
                 
                 // Destruimos el disparo
                 // No colocar solo Destroy()
                 //sino eli$$anonymous$$ara el Script
                 Destroy(disparo.gameObject);
                 
                 if (ps <= 0)
                 {
                     // $$anonymous$$uerto + EfectoEspecial de particulas, += score
                     
                     scoreScript.AddScore(ScoreValue);
                     EfectosEspecialesScript.Instancia.ExplosionBoss1(transform.position);
                     EfectosDeSonido.Instancia.ReproducirSonidoBossExplosion();
                     Destroy(gameObject);
                     Camera.main.GetComponent<CargarNivelCamaraScript>().myLoading(); 
                     scoreScript.StoreHighscore(HighScoreValue);
                 }
                 
             }
         }
     }
 
 }
avatar image mlabarca · Aug 24, 2014 at 02:56 PM 0
Share

When are you call StoreHighScore? also, It seems as you are only using that newHighscore > oldHighscore comparison to set the playerprefs, and then you ALWAYS set the highScore to the newHighScore parameter of StoreHighscore. Unless you have another check in place before you call StoreHighScore, it is likely that your current highscore is different than the one stored in PlayerPrefs.

Edit: Now that I see the other script: Where is HighScoreValue initialized there? also, as I said, there is no check in place to see if that value is higher than the current highscore.

4 Replies

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

Answer by zharik86 · Aug 25, 2014 at 07:17 AM

In your second script you initialization not variable "HighScoreValue". It's equal 0 always. But this variable isn't necessary to you. In your first script there is "score" variable. It also should be used. I will rewrite parts of a code which change:

  public class ScoreScript : MonoBehaviour {
   private int score;
   ...
   public void StoreHighscore() { //Use "score" variable
    int oldHighscore = PlayerPrefs.GetInt("highscore", 0);  
    if(score > oldHighscore)
     PlayerPrefs.SetInt("highscore", score);
    highScore = score;
    UpdateScore ();
   }
  }

And second script:

  public class SaludBossScript : MonoBehaviour { 
   ...
   void OnTriggerEnter2D(Collider2D collider) {
    ...
    if (ps <= 0) {
     // Muerto + EfectoEspecial de particulas, += score
 
     scoreScript.AddScore(ScoreValue); //you change score value in your script
     EfectosEspecialesScript.Instancia.ExplosionBoss1(transform.position);
     EfectosDeSonido.Instancia.ReproducirSonidoBossExplosion();
     Destroy(gameObject);
     Camera.main.GetComponent<CargarNivelCamaraScript>().myLoading(); 
     scoreScript.StoreHighscore(); //checks "score" in your script
    }
    ...
   }
  }

I hope that it will help you.

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
avatar image
1

Answer by praveee · Aug 24, 2014 at 02:23 PM

Try to getInt without default value.

 int oldHighscore = PlayerPrefs.GetInt("highscore");  
  
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
avatar image
1

Answer by mlabarca · Aug 24, 2014 at 03:00 PM

When are you call StoreHighScore? also, It seems as you are only using that newHighscore > oldHighscore comparison to set the playerprefs, and then you ALWAYS set the highScore to the newHighScore parameter of StoreHighscore. Unless you have another check in place before you call StoreHighScore, it is likely that your current highscore is different than the one stored in PlayerPrefs.

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
avatar image
0

Answer by Ryujose · Aug 25, 2014 at 01:12 PM

Thank you zharik86, it Works, the problema it was that I doesn't call the int score variable to highscore and I wanted to call highscore as a newvaluehighscore and return always 0. Am I wrong?

Have a nice day you all!

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 zharik86 · Aug 25, 2014 at 06:34 PM 1
Share

@Ryujose Yes you are right, the variable was always 0.

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

24 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

high scores scene 2 Answers

PlayerPrefs 2 Answers

Saving Highscore 2 Answers

Problem with High Score System using PlayerPrefs [C#] 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