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 12, 2019.

  1. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    public void OnDeath()
    {
    isDead = true;
    if(PlayerPrefs.GetFloat("Highscore1motomadness") < score)
    PlayerPrefs.SetFloat("Highscore1motomadness", score);

    isDead = true;
    if (PlayerPrefs.GetFloat("Highscore2Desert") < score)
    PlayerPrefs.SetFloat("Highscore2Desert", score);


    deathMenu.ToggleEndMenu(score);
    }


    I'm not sure how to set the isDead to a specific scene when I get a high score in moto madness it also sets it in desert
     
  2. AlexN2805

    AlexN2805

    Joined:
    Nov 27, 2019
    Posts:
    33
    You need to either have a way to know what scene the player was playing on, so you can do something like:
    Code (CSharp):
    1. public void OnDeath()
    2. {
    3.    // guessing you use the isDead boolean somewhere else, otherwise it's unnecessary here.
    4.    isDead = true;
    5.  
    6.    if (PlayerPrefs.GetFloat("ActiveScene") == "Motomadness")
    7.    {
    8.       if(PlayerPrefs.GetFloat("Highscore1motomadness") < score)
    9.           PlayerPrefs.SetFloat("Highscore1motomadness", score);
    10.    }
    11.  
    12.    if (PlayerPrefs.GetFloat("ActiveScene") == "Desert")
    13.    {
    14.        if (PlayerPrefs.GetFloat("Highscore2Desert") < score)
    15.            PlayerPrefs.SetFloat("Highscore2Desert", score);
    16.    }
    17.  
    18.    deathMenu.ToggleEndMenu(score);
    19. }
    or.. you need to implement a specific isDead boolean per scene. So isDeadInMotoMadness and isDeadInDesert etc. I suggest you go with the first though. So when a player plays a specific scene set the "ActiveScene" to that scene and go from there.
     
  3. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    I applied what you said and got this error "Assets/Scripts/Score.cs(64,25): error CS0019: Operator `==' cannot be applied to operands of type `float' and `string'."
     
  4. AlexN2805

    AlexN2805

    Joined:
    Nov 27, 2019
    Posts:
    33
    It's because in my bit of mockup code I'm using GetFloat("ActiveScene") .. which will probably return nothing, since I made up the variable with the string "Motomadness". It's still early morning.
    You will need to implement the "ActiveScene" bit yourself, like you did with "highscore1motomadness" and "HIghscore2Desert".

    My aim was to give you an idea on how to fix it, not a copy-paste solution.
     
  5. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    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.     public float scene1 = "motomadness";
    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.SetFloat("ActiveScene", scene1);
    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 (PlayerPrefs.GetFloat("ActiveScene") == "motomadness")
    60.         {
    61.             if (PlayerPrefs.GetFloat("Highscore1motomadness") < score)
    62.                 PlayerPrefs.SetFloat("Highscore1motomadness", score);
    63.         }
    64.  
    65.         if (PlayerPrefs.GetFloat("ActiveScene") == "Desert")
    66.         {
    67.             if (PlayerPrefs.GetFloat("Highscore2Desert") < score)
    68.                 PlayerPrefs.SetFloat("Highscore2Desert", score);
    69.         }
    70.  
    71.  
    72.         deathMenu.ToggleEndMenu(score);
    73.     }
    74.  
    75. }
    [ICODE][ICODE][ICODE]
    [/ICODE][/ICODE][/ICODE]


    this is the full score code I tried messing with it to get it to work but I still get that error and an error for the setfloat ActiveScene line which i tried to fix using the public float scene1 = motomadness and i get this error as well as the other two: Assets/Scripts/Score.cs(12,27): error CS0029: Cannot implicitly convert type `string' to `float'.
     
  6. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Change the string to a number, or use PlayerPrefs.SetString
     
  7. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    sorry I'm new to this, I changed the string to a number and it cleared the last error but I still have the == error.