Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Help setting high score for different levels.

Discussion in 'Scripting' started by jbowers74321, Dec 20, 2019.

  1. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    I fixed my errors but it's still not setting a high score for each level it's only setting a high score on Desert, what can I do to remedy this? Here is my score code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Net;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class Score : MonoBehaviour
    9. {
    10.     private float score = 0.0f;
    11.    
    12.  
    13.     private int difficultyLevel = 1;
    14.     private int maxDifficultyLevel = 10;
    15.     private int scoreToNextLevel = 10;
    16.  
    17.     private bool isDead = false;
    18.  
    19.     public Text scoreText;
    20.     public DeathMenu deathMenu;
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         PlayerPrefs.SetFloat("Highscore1motomadness", score); //For scene 1
    26.         PlayerPrefs.SetFloat("Highscore2Desert", score); //For scene 2
    27.         PlayerPrefs.SetString("ActiveScene", "motomadness");
    28.         PlayerPrefs.SetString("ActiveScene", "Desert");
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update()
    33.     {
    34.         if (isDead)
    35.             return;
    36.  
    37.         if (score >= scoreToNextLevel)
    38.             LevelUp();
    39.  
    40.         score += Time.deltaTime * difficultyLevel;
    41.         scoreText.text = ((int)score).ToString ();
    42.        
    43.     }
    44.  
    45.     void LevelUp()
    46.     {
    47.         if (difficultyLevel == maxDifficultyLevel)
    48.             return;
    49.  
    50.         scoreToNextLevel *= 2;
    51.         difficultyLevel++;
    52.  
    53.         GetComponent<playermotor>().SetSpeed(difficultyLevel);
    54.     }
    55.  
    56.     public void OnDeath()
    57.     {
    58.         isDead = true;
    59.  
    60.         if (PlayerPrefs.GetString("ActiveScene") == "motomadness")
    61.         {
    62.             if (PlayerPrefs.GetFloat("Highscore1motomadness") < score)
    63.                 PlayerPrefs.SetFloat("Highscore1motomadness", score);
    64.         }
    65.  
    66.         if (PlayerPrefs.GetString("ActiveScene") == "Desert")
    67.         {
    68.             if (PlayerPrefs.GetFloat("Highscore2Desert") < score)
    69.                 PlayerPrefs.SetFloat("Highscore2Desert", score);
    70.         }
    71.  
    72.  
    73.         deathMenu.ToggleEndMenu(score);
    74.     }
    75.  
    76. }
    77.  
    and here is how I save my high scores on my level select screen

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class LevelSelect : MonoBehaviour
    8. {
    9.  
    10.     public Text foresthscoreText;
    11.     public Text deserthscoreText;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.  
    17.         foresthscoreText.text = ((int)PlayerPrefs.GetFloat("Highscore1motomadness")).ToString();
    18.         deserthscoreText.text = ((int)PlayerPrefs.GetFloat("Highscore2Desert")).ToString();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.  
    25.     }
     
  2. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
  3. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    if I do that what do I set lines 60 and 66 to?
     
  4. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    I changed it to this and it still seems to be overriding the score once I get a score in Desert, motomadness's score disappears and vice versa.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Net;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class Score : MonoBehaviour
    9. {
    10.     private float score = 0.0f;
    11.    
    12.  
    13.     private int difficultyLevel = 1;
    14.     private int maxDifficultyLevel = 10;
    15.     private int scoreToNextLevel = 10;
    16.     private Scene scene;
    17.     private bool isDead = false;
    18.  
    19.     public Text scoreText;
    20.     public DeathMenu deathMenu;
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         PlayerPrefs.SetFloat("Highscore1motomadness", score); //For scene 1
    26.         PlayerPrefs.SetFloat("Highscore2Desert", score); //For scene 2
    27.         Scene scene = SceneManager.GetActiveScene();
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         if (isDead)
    34.             return;
    35.  
    36.         if (score >= scoreToNextLevel)
    37.             LevelUp();
    38.  
    39.         score += Time.deltaTime * difficultyLevel;
    40.         scoreText.text = ((int)score).ToString ();
    41.        
    42.     }
    43.  
    44.     void LevelUp()
    45.     {
    46.         if (difficultyLevel == maxDifficultyLevel)
    47.             return;
    48.  
    49.         scoreToNextLevel *= 2;
    50.         difficultyLevel++;
    51.  
    52.         GetComponent<playermotor>().SetSpeed(difficultyLevel);
    53.     }
    54.  
    55.     public void OnDeath()
    56.     {
    57.         isDead = true;
    58.  
    59.         if (SceneManager.GetActiveScene().name == "motomadness")
    60.         {
    61.             if (PlayerPrefs.GetFloat("Highscore1motomadness") < score)
    62.                 PlayerPrefs.SetFloat("Highscore1motomadness", score);
    63.         }
    64.  
    65.         if (SceneManager.GetActiveScene().name == "Desert")
    66.         {
    67.             if (PlayerPrefs.GetFloat("Highscore2Desert") < score)
    68.                 PlayerPrefs.SetFloat("Highscore2Desert", score);
    69.         }
    70.  
    71.  
    72.         deathMenu.ToggleEndMenu(score);
    73.     }
    74.  
    75. }
    76.